本人学习Qt 不到一周,之前几乎没有任何基础,所以经常在一些小问题上卡住很久,现在有碰到一个弱智问题,劳请大家帮帮忙啊~~~~
我用的是Qt Create 在Windows下入门,
自己的问法好像不对,大家似乎看到我的代码就烦了~~ 这么久都没人理…………
其实主要的问题就是 在ui_mainwindow.h中声明了两个类指针 QButton *btnClose 和 QWidget *centralWidget 但是在mainwindow.cpp中使用时却无法通过编译,centralWidget可以,但btnClose却出现错误,说是没有声明,而且在mainwindow.cpp中编写的时候,也无法检测到………………
拜托了………………
代码:
######################
main.cpp
######################
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
##################
mainwindow.cpp
##################
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
QObject::connect(btnClose, SIGNAL(click()), centralWidget, SLOT(quit()));
}
MainWindow::~MainWindow()
{
delete ui;
}
#######################
mainwindow.h
#######################
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
namespace Ui
{
class MainWindowClass;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindowClass *ui;
};
#endif // MAINWINDOW_H
######################
ui_mainwindow.h
######################
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
#include "ui_mainWindow.h"
QT_BEGIN_NAMESPACE
class Ui_MainWindowClass
{
public:
QWidget *centralWidget;
QLabel *label;
QPushButton *btnClose;
QPushButton *btnChange;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindowClass)
{
if (MainWindowClass->objectName().isEmpty())
MainWindowClass->setObjectName(QString::fromUtf8("MainWindowClass"));
MainWindowClass->resize(421, 246);
centralWidget = new QWidget(MainWindowClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
label = new QLabel(centralWidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(30, 10, 361, 131));
btnClose = new QPushButton(centralWidget);
btnClose->setObjectName(QString::fromUtf8("btnClose"));
btnClose->setGeometry(QRect(30, 160, 120, 23));
…………
…………
};
namespace Ui {
class MainWindowClass: public Ui_MainWindowClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
错误的地方在mainwindow.cpp中
QObject::connect(btnClose, SIGNAL(click()), centralWidget, SLOT(quit()));
这里
error: `btnClose' was not declared in this scope
说是btnClose没有声明,但是在ui_mainwindow里面明明已经有声明了啊~~~~~
假如把mainwindow.h中
class MainWindow : public QMainWindow
改为
class MainWindow : public QMainWindow,public Ui_MainWindowClass
就会出现这种错误
error: expected class-name before '{' token
这总低级的错误在网上实在搜不到相同的案例,所以只能来这问了,各位勿笑~~~~~~~~~~~~~~~
[ 此帖被whisper215在2009-03-03 19:21重新编辑 ]