[問題] Qt connect 的slot不起作用

看板C_and_CPP作者 (沒有存在感的人)時間10年前 (2014/04/24 16:17), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/2 (看更多)
開發平台(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討論區?台灣好像做這個的不多 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.209.185.140 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1398327420.A.159.html

04/25 03:06, , 1F
class定義裡面要加上"slot:", 該函式才會被定義成slot
04/25 03:06, 1F

04/25 10:16, , 2F
Qt 的一些 keyword 是給 moc 看的,本身對 C++ compiler
04/25 10:16, 2F

04/25 10:16, , 3F
無作用(定義為空白)
04/25 10:16, 3F

04/26 04:32, , 4F
感謝,已經解決了!
04/26 04:32, 4F
文章代碼(AID): #1JMCXy5P (C_and_CPP)
文章代碼(AID): #1JMCXy5P (C_and_CPP)