[英]connect QAction to slot
我試圖理解為什么QObject :: connect有時可以完成這項工作,為什么有時卻不能完成這項工作。
我真的很樂意提供任何幫助,我已經做了很多google / Documentation閱讀/,並且正在尋找問題的可能重復內容,重復案例無法解決我的問題
我有2個Qt項目非常相似的代碼,並且由於幾個小時,我試圖弄清為什么它不起作用。
工作測試項目:
工具欄
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QMainWindow>
#include <QApplication>
class Toolbar : public QMainWindow
{
Q_OBJECT
public:
Toolbar(QWidget *parent = 0);
public slots:
void dosmt();
signals:
void test();
void test2();
private:
static bool button1;
};
#endif // TOOLBAR_H
工具欄
#include "toolbar.h"
#include <QToolBar>
#include <QIcon>
#include <QAction>
#include <QLabel>
#include <iostream>
bool Toolbar::button1=false;
Toolbar::Toolbar(QWidget *parent): QMainWindow(parent)
{
QString path="../res/login_photo.png";
QPixmap newpix(path);
QPixmap openpix("../res/global.png");
QPixmap quitpix("new.png");
QToolBar *toolbar = addToolBar("main toolbar");
QAction *hallo=toolbar->addAction(QIcon(newpix), "1");
toolbar->addAction(QIcon(openpix), "Open File");
toolbar->addSeparator();
QAction *quit = toolbar->addAction(QIcon(quitpix),
"Quit Application");
connect(hallo,SIGNAL(triggered()),this,SLOT(dosmt()));
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void Toolbar::dosmt()
{
button1=!button1;
if(button1){
emit test();
}else{
emit test2();
}
}
現在,我想改善布局,以便可以放入所有可能需要的元素,因此,如果我注釋了不起作用的連接信號插槽線,我就創建了一個新項目,該項目正在運行。 工具欄基本上只是重命名為mainwindow
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void dosmt();
void h1();
signals:
void s1();
};
主窗口
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QToolBar>
#include <QApplication>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QPixmap newpix("../res/login_photo.png");
QPixmap openpix("../res/global.png");
QPixmap quitpix("new.png");
QToolBar *toolbar = new QToolBar;
QAction* hallo=toolbar->addAction(QIcon(newpix), "Page1");
toolbar->addAction(QIcon(openpix), "Open File");
QAction *quit = toolbar->addAction(QIcon(quitpix),"Quit Application");
這是我的問題下面的錯誤信息
connect(hallo,SIGNAL(triggered()),this,SLOT(h1()));
沒問題的時候:
connect(this,SIGNAL(s1()),this,SLOT(h1()));
休息:
QVBoxLayout * mvbox = new QVBoxLayout;
mvbox->addWidget(toolbar);
setCentralWidget(new QWidget);
centralWidget()->setLayout(mvbox);
}
void MainWindow::dosmt()
{
qDebug("happ");
}
void MainWindow::h1()
{
qDebug("happ");
}
MainWindow::~MainWindow()
{
}
錯誤(Fehler)消息:
/cpp/qt/GUItest1/mainwindow.cpp:19:
Fehler: no matching function for call to 'MainWindow::connect(QAction*&, const char*, MainWindow* const, const char*)'
connect(hallo,SIGNAL(triggered()),this,SLOT(h1()));
followed by a bunch of not helping stuff i just took the ones that look remotly usefull
Qt/5.4/gcc_64/include/QtCore/qobject.h:213: Fehler:
no type named'Object' in 'struct QtPrivate::FunctionPointer<const char*>'
^
/Qt/5.4/gcc_64/include/QtCore/qobject.h:254: Fehler: invalid use of incomplete type 'struct QtPrivate::QEnableIf<false, QMetaObject::Connection>'
/Qt/5.4/gcc_64/include/QtCore/qglobal.h:1056: Fehler: declaration of 'struct QtPrivate::QEnableIf<false, QMetaObject::Connection>'
template <bool B, typename T = void> struct QEnableIf;
^
/Qt/5.4/gcc_64/include/QtCore/qobject.h:293:
Fehler: no type named 'Object' in 'struct QtPrivate::FunctionPointer<const char*>'
通過玩耍,我發現問題出在信號QAction之內,因為來自Mainwindow的信號可以連接到來自Mainwindow的插槽
項目一和項目二之間的唯一區別似乎是如何添加工具欄
addToolBar
與
QToolBar *toolbar = new QToolBar; and
mvbox->addWidget(toolbar);
您忘記在mainwindow.cpp中包含QAction標頭。
#include <QAction>
它應該編譯您的程序而沒有錯誤。
為什么不使用SLOTS
?
toolbar = addToolBar("main toolbar");
toolbar->addAction("Open", this, SLOT(open_file()) );
toolbar->addAction("New", this, SLOT(new_file());
toolbar->addAction("Quit", this, SLOT(quit_application());
在header
:
[...]
public slots:
void open_file();
void new_file();
void quit_application();
[...]
插槽是QT的最佳做法,您應該使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.