求教啊,为什么窗口一闪而逝?Qt新手,照着书上打的
跪求各位高手指导如何留住窗口
代码如下,h文件和main十分简单,就不上来了,主要是类的定义,继承于QDialog的类
#include <QtGui>#include "extension.h"Extension::Extension(QWidget *parent) : QDialog(parent){ setWindowTitle(tr("Extension Window try")); createBaseInfo(); createDetailInfo(); QVBoxLayout * layout = new QVBoxLayout; layout->addWidget(baseWidget); layout->addWidget(detailWidget); layout->setSizeConstraint(QLayout::SetFixedSize); layout->setSpacing(10); setLayout(layout);}void Extension::createBaseInfo(){ baseWidget = new QWidget; QLabel * nameLabel = new QLabel(tr("Name:")); QLineEdit * nameEdit = new QLineEdit; QLabel * sexLabel = new QLabel(tr("Sex:")); QComboBox * sexCombo = new QComboBox; sexCombo->addItem(tr("Male")); sexCombo->addItem(tr("Female")); QPushButton * okBtn = new QPushButton(tr("OK")); QPushButton * detailBtn = new QPushButton(tr("detail")); connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotExtension())); QDialogButtonBox * btnBox = new QDialogButtonBox(Qt::Vertical); btnBox->addButton(okBtn,QDialogButtonBox::ActionRole); btnBox->addButton(detailBtn,QDialogButtonBox::ActionRole); baseWidget->show();}void Extension::createDetailInfo(){ detailWidget = new QWidget; QLabel * ageLabel = new QLabel(tr("Age:")); QLineEdit * ageEdt = new QLineEdit; ageEdt->setText(tr("20")); QLabel * departmentLabel = new QLabel(tr("Department:")); QComboBox * departmentCombo = new QComboBox; departmentCombo->addItem(tr("test 1")); departmentCombo->addItem(tr("test 2")); departmentCombo->addItem(tr("test 3")); departmentCombo->addItem(tr("test 3")); QLabel * emailLabel = new QLabel(tr("e-mail")); QLineEdit * emailEdit = new QLineEdit; emailEdit->setText(tr("@")); detailWidget->hide();}void Extension::slotExtension(){ if(detailWidget->isHidden()) { detailWidget->show(); } else { detailWidget->hide(); }}
{ Dialog *dlg = new Dialog(); dlg->show();}
[解决办法]
我将LZ的程序补充完整了,但我还是看不出哪里错了,等待高手
#include <QtGui>class Extension : public QDialog{private: QWidget *baseWidget; QWidget *detailWidget;public: Extension(); void createBaseInfo(); void createDetailInfo(); private slots: void slotExtension();};Extension::Extension() : QDialog(){ setWindowTitle(tr("Extension Window try")); createBaseInfo(); createDetailInfo(); QVBoxLayout * layout = new QVBoxLayout; layout->addWidget(baseWidget); layout->addWidget(detailWidget); layout->setSizeConstraint(QLayout::SetFixedSize); layout->setSpacing(10); setLayout(layout);}void Extension::createBaseInfo(){ baseWidget = new QWidget; QLabel * nameLabel = new QLabel(tr("Name:")); QLineEdit * nameEdit = new QLineEdit; QLabel * sexLabel = new QLabel(tr("Sex:")); QComboBox * sexCombo = new QComboBox; sexCombo->addItem(tr("Male")); sexCombo->addItem(tr("Female")); QPushButton * okBtn = new QPushButton(tr("OK")); QPushButton * detailBtn = new QPushButton(tr("detail")); connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotExtension())); QDialogButtonBox * btnBox = new QDialogButtonBox(Qt::Vertical); btnBox->addButton(okBtn,QDialogButtonBox::ActionRole); btnBox->addButton(detailBtn,QDialogButtonBox::ActionRole); baseWidget->show();}void Extension::createDetailInfo(){ detailWidget = new QWidget; QLabel * ageLabel = new QLabel(tr("Age:")); QLineEdit * ageEdt = new QLineEdit; ageEdt->setText(tr("20")); QLabel * departmentLabel = new QLabel(tr("Department:")); QComboBox * departmentCombo = new QComboBox; departmentCombo->addItem(tr("test 1")); departmentCombo->addItem(tr("test 2")); departmentCombo->addItem(tr("test 3")); departmentCombo->addItem(tr("test 3")); QLabel * emailLabel = new QLabel(tr("e-mail")); QLineEdit * emailEdit = new QLineEdit; emailEdit->setText(tr("@")); detailWidget->hide();}void Extension::slotExtension(){ if(detailWidget->isHidden()) { detailWidget->show(); } else { detailWidget->hide(); }}int main(int argc, char *argv[]){ QApplication app(argc, argv); Extension w; w.show(); return app.exec();}
[解决办法]