I'm trying to understand why QObject::connect sometimes does the job and why sometimes it does not.
i would be really happy about anykind of help, i already did a lot of google/Documentation reading/ and looking for possible dublicates of the question, the duplicates cases did not solve my problem
i have 2 Qt Projects very similar code and since some hours i try to figure out why its not working.
the working testproject:
toolbar.h
#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
toolbar.cpp
#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();
}
}
now i wanted to improve the layout so that i can put in all the elements i may need so i made a new project that is working if i comment the not working connec signal slot line out. toolbar is basicly just renamed to 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();
};
mainwindow.cpp
#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");
here is my problem error message below
connect(hallo,SIGNAL(triggered()),this,SLOT(h1()));
no problem when:
connect(this,SIGNAL(s1()),this,SLOT(h1()));
rest:
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()
{
}
Error(Fehler) Message:
/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*>'
By playing around i found out that the problem lies within the signal QAction, because signals from Mainwindow can be connected to slots from mainwindow
the only diffence between project one and two seems to be how the toolbar is added
addToolBar
vs
QToolBar *toolbar = new QToolBar; and
mvbox->addWidget(toolbar);
You have forgot to include the QAction header in mainwindow.cpp.
#include <QAction>
It should compiler your program without errors.