[问题] Qt connect 的slot不起作用

楼主: wtchen (没有存在感的人)   2014-04-24 16:16:57
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux Mint 15 + Qt 5.01
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
刚开始学Qt,想要把从github上找到的open source library GUI化
目前正在设计QMenu跟QAction,希望用鼠标点某个Menu上的Action后
可以把Dialog叫出来设定
*[36m程式码(Code):(请善用置底文网页, 记得排版) *[m
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "include/Dialog_Units.h"
namespace Ui {
class MainWindow;
}
class QAction;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initConnect();
private:
Ui::MainWindow *ui;
QMenu *m_option;
QAction *option_Units;
Dialog_Units* Connect_Option_Units();
};
#endif // MAINWINDOW_H
MainWindow.cpp :
#include <QMenu>
#include <QToolBar>
#include <QDialog>
#include "include/Dialog_Units.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_option = menuBar()->addMenu(tr("&Option"));
option_Units = new QAction(tr("&Units"), this);
option_Units->setStatusTip(tr("Set units"));
m_option->addAction(option_Units);
connect(option_Units, SIGNAL(triggered()), this,
SLOT(Connect_Option_Units()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Dialog_Units* MainWindow::Connect_Option_Units()
{
Dialog_Units *dialog = new Dialog_Units(this);
dialog->showNormal();
return dialog;
}
void MainWindow::initConnect()
{
connect(option_Units, SIGNAL(triggered()), this,
SLOT(Connect_Option_Units()));
}
Dialog_Unit.h :
#ifndef DIALOG_UNITS_H
#define DIALOG_UNITS_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
class Dialog_Units : public QDialog
{
Q_OBJECT
public:
Dialog_Units(QWidget *parent = 0);
~Dialog_Units();
private slots:
void buttonOKClicked();
void buttonCancelClicked();
private:
QLabel *label;
QComboBox *selectUnits;
QPushButton *buttonOK;
QPushButton *buttonCancel;
};
#endif // DIALOG_UNITS_H
Dialog_Units.cpp:
#include <QtGui>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "CoolProp.h"
#include "include/Dialog_Units.h"
Dialog_Units::Dialog_Units(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Units"));
selectUnits = new QComboBox;
selectUnits->addItem(tr("SI"));
selectUnits->addItem(tr("kSI"));
buttonOK = new QPushButton(tr("&Ok"));
buttonCancel = new QPushButton(tr("&Cancel"));
connect(buttonOK, SIGNAL(clicked()), this, SLOT(buttonOKClicked()));
connect(buttonCancel, SIGNAL(clicked()), this,
SLOT(buttonCancelClicked()));
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(selectUnits);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(buttonOK);
rightLayout->addWidget(buttonCancel);
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Set Units"));
setFixedHeight(sizeHint().height());
}
Dialog_Units::~Dialog_Units()
{
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
#include "CoolProp.h"
#include "HumidAirProp.h"
#include "CPState.h"
#include <iostream>
#include <stdlib.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("QtCoolProp");
w.resize(1280,700);
w.show();
return app.exec();
}
预期的正确结果(Expected Output):
执行程式后,下拉Option点Units会出现Dialog_Units指定的对话方块
错误结果(Wrong Output):
对话方块无法叫出
错误讯息:
Starting /home/wtchen/qt5/build-QtCoolProp-Desktop-Debug/QtCoolProp...
QObject::connect: No such slot MainWindow::Connect_Option_Units() in
../QtCoolProp/mainwindow.cpp:41
QObject::connect: (receiver name: 'MainWindow')
/home/wtchen/qt5/build-QtCoolProp-Desktop-Debug/QtCoolProp exited with code 0
补充说明(Supplement):
请问哪里有资讯比较丰富的Qt讨论区?台湾好像做这个的不多
作者: wolfer (向上提升/向下沉沦)   2014-04-25 03:06:00
class定义里面要加上"slot:", 该函式才会被定义成slot
作者: Bencrie   2014-04-25 10:16:00
Qt 的一些 keyword 是给 moc 看的,本身对 C++ compiler无作用(定义为空白)
楼主: wtchen (没有存在感的人)   2014-04-26 04:32:00
感谢,已经解决了!

Links booklink

Contact Us: admin [ a t ] ucptt.com