当前位置:编程学习 > wap >>

Qt 部件窗口显示问题

添加的两个按键没有在Dialog中显示,跟QVBoxQVBoxLayout *rightLayout = new QVBoxLayout(this) 有什么关系呢?把这两个按钮添加在QHBoxLayout *hlayout,可以显示。求大神!!!

FindDialog::FindDialog(QWidget *parent) :
    QDialog(parent)
{


    label= new QLabel(tr("Find &Case"));
    lineEdit = new QLineEdit(this);
    label->setBuddy(lineEdit);//绑定为伙伴关系

    caseCheckBox = new QCheckBox(tr("Match &Case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton(tr("&Close"));

    connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(enableFindButton(QString)));//当输入搜索内容是,find按键使能。
    connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

    QHBoxLayout *hlayout = new QHBoxLayout;
    hlayout->addWidget(label);
    hlayout->addWidget(lineEdit);

    QVBoxLayout *leftLayout = new QVBoxLayout(this);
    leftLayout->addLayout(hlayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout *rightLayout = new QVBoxLayout(this); ??这里的布局没有用,
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch(1);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
}
qt dialog QVBoxLayout --------------------编程问答--------------------     findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton(tr("&Close"));
这是newbutton时候的代码,没有指定parent,两个button不是这个dialog上的控件,当然不会显示出来。
添加进布局之后,就为两个button指定了parent所以会看到。
可以做下实验:
    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton(tr("&Close"));
增加两行:
findButton->show();
closeButton->show();
看看效果。
    
--------------------编程问答-------------------- QVBoxLayout *rightLayout = new QVBoxLayout(this); ??这里的布局没有用,
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch(1);
这里不是已经把两个button添加进布局了吗?像lineEdit不添加进布局的话,确实是不会显示。但你这方法可以试试。 --------------------编程问答-------------------- 增加两行:
findButton->show();
closeButton->show();
效果不行啊,这样做的结果就是另外生成两个button的对话框。
我还尝试过这样做:
findButton = new QPushButton(this);
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton(this);
指定parent,虽然显示了,但是确实两个button重叠一起显示,没有得到布局想要得结果。我觉得是
QVBoxLayout *rightLayout = new QVBoxLayout(this); 
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch(1);

这个布局没有效果。 --------------------编程问答-------------------- 肯定是被覆盖掉了 --------------------编程问答-------------------- rightLayout->addStretch(1);
把上面这句话删了看看怎么样,怎么就在这加了个弹簧呢??可能加了后被上面原有的widget覆盖了。

还有,leftLayout 和 rightLayout的定义中,为什么后面都要加了this??一般不是都没有吗??删了吧。。。 --------------------编程问答-------------------- "rightLayout->addStretch(1);
把上面这句话删了看看怎么样,怎么就在这加了个弹簧呢??可能加了后被上面原有的widget覆盖了。

还有,leftLayout 和 rightLayout的定义中,为什么后面都要加了this??一般不是都没有吗??删了吧。。。"

我的版本是4.8.4的。你说的这些之前我就试过,结果还是一样。我怀疑是不是4.8.4的不支持有多个QVBoxLayout或者QHBoxLayout的定义啊?呵呵。 --------------------编程问答-------------------- 你还是照着教程打代码试试吧,好多地方和教程不一样耶。。。

试试规定下lineEdit的长度。

addStretch(1)里面的1去掉试试。。还有那些connect里的SIGNAL和SLOT的参数很多跟教程不一样~~ --------------------编程问答-------------------- QVBoxLayout *leftLayout = new QVBoxLayout(this);
QVBoxLayout *rightLayout = new QVBoxLayout(this); ??这里的布局没有用,
把两个this删掉即可。 --------------------编程问答-------------------- 谢谢各位!问题找到了,按6楼和8楼的说法做,去掉两个this就OK了!
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,