上午刚刚做了一个练习只是简单的操作,但是Build不过去,在头文件中将Q_OBJECT删除后可以正常build。不知道具体问题出在哪里了,希望各位能帮忙解答,谢谢了。
头文件:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class MyWidget:public QDialog
{
Q_OBJECT
public:
MyWidget(QWidget *parent=0);
public slots:
void okButtonClicked();
void noButtonClicked();
private:
QLabel *nameLabel;
QLabel *pwdLabel;
QLineEdit *nameEdit;
QLineEdit *pwdEdit;
QPushButton *okButton;
QPushButton *noButton;
};
#endif // MYWIDGET_H
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
程序:
#include <QtGui>
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent)
:QDialog(parent)
{
nameLabel = new QLabel;
nameLabel->setText("Name:");
nameEdit = new QLineEdit;
nameLabel->setBuddy(nameEdit);
pwdLabel = new QLabel;
pwdLabel->setText("Password:");
pwdEdit = new QLineEdit;
pwdEdit->setEchoMode(QLineEdit::Password);
pwdLabel->setBuddy(pwdEdit);
okButton = new QPushButton;
okButton->setText(tr("Ok"));
noButton = new QPushButton;
noButton->setText(tr("No"));
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QHBoxLayout *centerLayout = new QHBoxLayout;
centerLayout->addWidget(pwdLabel);
centerLayout->addWidget(pwdEdit);
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addWidget(okButton);
bottomLayout->addWidget(noButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addLayout(centerLayout);
mainLayout->addLayout(bottomLayout);
connect(this->okButton,SIGNAL(clicked()),this,SLOT(okButtonClicked()));
connect(this->noButton,SIGNAL(clicked()),this,SLOT(noButtonClicked()));
setLayout(mainLayout);
}
void MyWidget::okButtonClicked(){
if(nameEdit->text().isEmpty()&&pwdEdit->text().isEmpty()){
QMessageBox::warning(this,"warming","",QMessageBox::Ok);
}else{
QMessageBox::information(this,"welcome",
tr("Hello {0}").arg(nameEdit->text()),QMessageBox::Ok,QMessageBox::Ok);
}
}
void MyWidget::noButtonClicked(){
int rel = QMessageBox::information(this,"do you want to close the app.",
"do you want to close the app.",
QMessageBox::Ok,QMessageBox::Ok);
if(rel==QMessageBox::Ok){
this->close();
}
}