[問題] 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