Gre*_*g K 0 c++ user-interface qt qt4
我刚开始用Qt.尽管今天晚上花了一些时间,但我仍在努力将我的UI设置代码移出main它自己的类.
#include <QtGui>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget *window = new QWidget;
QLabel *hw = new QLabel(QObject::tr("Hello World!"));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(hw);
window->setLayout(layout);
window->show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试创建自己的类并传递window给它但遇到编译错误.
main.cpp中:
#include <QtGui>
#include "hworld.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDialog *hWorld = new hWorld;
hWorld->show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
hworld.h:
#ifndef HWORLD_H
#define HWORLD_H
#include <QtGui>
class hWorld : public QDialog {
Q_OBJECT
public:
hWorld(QWidget *parent = 0);
~hWorld();
private:
void setup();
};
#endif // HWORLD_H
Run Code Online (Sandbox Code Playgroud)
hworld.cpp:
#include <QtGui>
#include "hworld.h"
hWorld :: hWorld(QWidget *parent) : QDialog(parent) {
setup();
}
hWorld :: ~hWorld() { }
void hWorld :: setup() {
QLabel *hw = new QLabel(QObject::tr("Hello World!"));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(hw);
setLayout(layout);
setWindowTitle("Test App");
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8: error: expected type-specifier before ‘hWorld’
main.cpp:8: error: cannot convert ‘int*’ to ‘QDialog*’ in initialization
main.cpp:8: error: expected ‘,’ or ‘;’ before ‘hWorld’
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
更改main,意味着这个编译,但我得到一个空白窗口(因为没有调用构造函数?):
QDialog hWorld;
hWorld.show();
Run Code Online (Sandbox Code Playgroud)
你不应该为类和实例化变量使用不同的名称吗?
QDialog *hWorld = new hWorld;
Run Code Online (Sandbox Code Playgroud)
你得到的错误的来源非常混乱,HWorld而不是用于类(例如),因为通常使用大写字母(上部驼峰套管)来启动类型名称.
此外,是从QWidget有QDialog目的的变化?