请教一个QT标准INT输入框的小问题
看书上的例子通过点击主界面的按钮弹出标准INT输入对话框,可是总是有异常自动退出,不知道为什么呢,麻烦大家帮帮忙 ,感激不尽了
上代码来:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
#include "inputdlg.h"
#include <QGridLayout>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private:
QPushButton *inputBtn;
InputDlg *inDlg;
QGridLayout *mainlayout;
private slots:
void showInputDlg();
};
#endif // DIALOG_H
//dialog.cpp
#include "dialog.h"
#include <QGridLayout>
#include <QPushButton>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
inputBtn =new QPushButton;
inputBtn->setText(tr("awrwarfwaf"));
mainlayout->addWidget(inputBtn,2,0);
connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInputDlg()));
}
void Dialog::showInputDlg()
{
inDlg=new InputDlg(this);
inDlg->show();
}
//inputdlg.h
#ifndef INPUTDLG_H
#define INPUTDLG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
class InputDlg : public QDialog
{
Q_OBJECT
public:
InputDlg(QWidget *parent = 0);
private slots:
void change();
private:
QLabel *time1;
QLabel *time2;
QPushButton *sztime;
QGridLayout *mainLayout;
};
#endif // INPUTDLG_H
//inputdlg.cpp
#include "inputdlg.h"
#include <QInputDialog>
InputDlg::InputDlg(QWidget *parent) :
QDialog(parent)
{
//setWindowTitle(tr("程序设置"));
time1=new QLabel;
time1->setText(tr("设置时间"));
time2=new QLabel;
time2->setText(tr("1"));
time2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
sztime=new QPushButton;
sztime->setText(tr("设置"));
mainLayout->addWidget(time1,0,0);
mainLayout->addWidget(time2,0,1);
mainLayout->addWidget(sztime,0,2);
connect(sztime,SIGNAL(clicked()),this,SLOT(change()));
}
void InputDlg::change()
{
bool ok;
int t = QInputDialog::getInt(this,tr("设置自动播放时间"),tr("请输入时间:"),time2->text().toInt(&ok),0,100,1,&ok);
if (ok)
time2->setText(QString(tr("%1")).arg(t));
}
//main.cpp
#include <QtGui/QApplication>
#include "dialog.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Dialog w;
w.show();
return a.exec();
}
谢谢 ,谢谢大家咯,感激不尽
[解决办法]
sztime->setText(tr("设置"));
mainLayout->addWidget(time1,0,0);
mainLayout->addWidget(time2,0,1);
mainLayout->addWidget(sztime,0,2);
仔细看,mainLayout同有new就使用了
[解决办法]
在InputDlg的构造函数里面加上这句mainLayout=new QGridLayout;