C++ Qt-事物如何协同工作?

C++ Qt-事物如何协同工作?,c++,qt,C++,Qt,下面,我将展示《使用Qt4进行C++GUI编程》一书中不同的应用程序文件,并对它们提出一些问题 main.cpp #include <QApplication> #include "gotocelldialog.h" //#include <QDialog> //#include "ui_gotocelldialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); //Ui::GoTo

下面,我将展示《使用Qt4进行C++GUI编程》一书中不同的应用程序文件,并对它们提出一些问题

main.cpp

#include <QApplication>
#include "gotocelldialog.h"
//#include <QDialog>
//#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//Ui::GoToCellDialog ui;
//QDialog *dialog = new QDialog;
GoToCellDialog *dialog = new GoToCellDialog;
//ui.setupUi(dialog);
dialog->show();
return app.exec();
}
#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QWidget *parent): QDialog(parent)
{
setupUi(this); //this: reference to the current class
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
QValidator *validator = new QRegExpValidator(regExp, this);
lineEdit->setValidator(validator);
connect(okButton, SIGNAL(clicked()),this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
#包括
#包括“gotocelldialog.h”
//#包括
//#包括“ui_gotocelldialog.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
//Ui::GoToCellDialog Ui;
//QDialog*dialog=新建QDialog;
GoToCellDialog*dialog=新建GoToCellDialog;
//ui.setupUi(对话框);
对话框->显示();
返回app.exec();
}
gotocelldialog.h

#ifndef GOTOCELLDIALOG_H //Check if GOTOCELLDIALOG_H has not been defined previously
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
class GoToCellDialog: public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialog(QWidget *parent = 0); 
private slots:
void on_lineEdit_textChanged();
};
#endif
\ifndef GOTOCELLDIALOG\u H//检查之前是否未定义GOTOCELLDIALOG\u H
#定义GOTOCELLDIALOG_H
#包括
#包括“ui_gotocelldialog.h”
类GoToCellDialog:PublicQDialog,PublicUI::GoToCellDialog
{
Q_对象
公众:
GoToCellDialog(QWidget*parent=0);
专用插槽:
在_lineEdit_textChanged()上无效;
};
#恩迪夫
gotocelldialog.cpp

#include <QApplication>
#include "gotocelldialog.h"
//#include <QDialog>
//#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//Ui::GoToCellDialog ui;
//QDialog *dialog = new QDialog;
GoToCellDialog *dialog = new GoToCellDialog;
//ui.setupUi(dialog);
dialog->show();
return app.exec();
}
#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QWidget *parent): QDialog(parent)
{
setupUi(this); //this: reference to the current class
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
QValidator *validator = new QRegExpValidator(regExp, this);
lineEdit->setValidator(validator);
connect(okButton, SIGNAL(clicked()),this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
#包括
#包括“gotocelldialog.h”
GoToCellDialog::GoToCellDialog(QWidget*parent):QDialog(parent)
{
setupUi(this);//this:对当前类的引用
QRegExp regExp(“[A-Za-z][1-9][0-9]{0,2}”);
QValidator*validator=新的QRegExpValidator(regExp,this);
lineEdit->setValidator(验证程序);
连接(确定按钮、信号(单击())、此、插槽(接受());
连接(取消按钮、信号(单击())、此、插槽(拒绝());
}
void GoToCellDialog::on_lineEdit_textChanged()
{
OK按钮->设置启用(lineEdit->hasAcceptableInput());
}
1-放置
GoToCellDialog(QWidget*parent=0)有什么用,尤其是它不允许传递父级

2-在main.cpp中,注释代码
ui.setupUi(对话框)明确显示我要设置的
ui
对话框。但是,在gotocelldialog.cpp中,您将看到
setupUi(这一点)
不确定
ui
我想将当前对象设置为。这里怎么用?在这种情况下,设置用户界面意味着什么

3-在gotocelldialog.cpp中,这一行是什么意思<代码>GoToCellDialog::GoToCellDialog(QWidget*父项):QDialog(父项)
。我不清楚我们是如何通过父母的。我们如何从这一行确定父级

4-在gotocelldialog.cpp中,是否有另一种写这行的方法:
gotocelldialog::gotocelldialog(QWidget*parent):QDialog(parent)
?而且,如果我们删除构造函数,我们如何编写它呢?它可以从
GoToCellDialog{…}
开始吗

谢谢你的努力。

< P> 1):这是基本的C++语法。这是一个默认参数。它可以在不传入指针的情况下调用构造函数,在这种情况下,0(即
NULL
)将被替换

对于2):请阅读Qt文档中的UI文件。这在那里得到了解释。(http://doc.qt.nokia.com/latest/designer-using-a-ui-file.html)(关于它的含义:请阅读您之前问题的答案、uic生成的代码以及文档)

3)再次说明这是基本C++。它以

parent
作为参数调用超类的构造函数

< 4):不,你不能用你想要的方式写C++。语法很严格,你必须遵守它


请在互联网上搜索C++的信息和教程,里面有好几百个好的参考文献。

< P>对不起,我不知道如何回答你的第二个问题,但是我会尝试回答其他问题。 1) -如果父项为0,则新窗口小部件(本例中为gotocelldialog)将成为一个窗口。如果指定了父窗口小部件,则创建的窗口小部件将成为父窗口内的子窗口。当其父窗口小部件被删除时,子窗口小部件也被删除

3) GoToCellDialog类是从QDialog类继承而来的,该类具有构造函数QDialog(QWidget*parent=0)。在这一行中,您使用父对象初始化基本对象,该父对象被传递给GoToCellDialog的ctor

4) 是的,你可以这样写:

GoToCellDialog(QWidget*父项){…}


但在这种情况下,您将错过在构建阶段将crteated GoToCellDialog对象设置为其他小部件的子窗口的机会,因为QDialog基本对象将在其ctor中使用QWidget*父参数的默认值,即0(请参见第1项)。

在前面的问题之后,我建议你坐下来,打开一本体面的C++书籍,然后再前进到QT。