Important: Please read the Qt Code of Conduct -

Need help with signals and slots

  • Hello everyone,

    I am a bit lost and confused here and would be very thankful if someone could help me.

    I want to call a function after a button is clicked. My program doesn't give me any errors and the Signal and the Slot is connected (I put a bool variable in front of the connect and checked its value. It was true. ), but the function undoOneMove3 won't execute (I don't get the text I put in the function. And I'm pretty sure the output commands are not the problem, because I tried them somewhere else in the program where they worked.)
    But I don't find the reason it doesn't work.

    Here are parts of the programm:


    #ifndef UNDOMOVE_H
    #define UNDOMOVE_H
    #include <stack>
    #include <QLabel>
    #include "dragwidget.h"
    class undoMove3 : public Drag3Widget
        explicit undoMove3(QWidget *parent = nullptr);
        static std::stack<Drag3Widget*> undoStack3;     
        static std::stack< int > undoCoord3x;          
        static std::stack< int > undoCoord3y;
        friend class Drag3Widget;
    public slots:
        void undoOneMove3();
    #endif // UNDOMOVE_H


    #include "undomove.h"
    #include <QTextStream>
    undoMove3::undoMove3( QWidget *parent ){ }
    // löscht den letzten Spielstein vom Spielfeld und legt ihn auf die Benutzerhand.
    void undoMove3::undoOneMove3(){
        if ( undoMove3::undoStack3.empty() )
            QTextStream out(stdout);
            out << QString("undoOneMove3-Function-Beginning");
            QLabel *newIcon = new QLabel( );
            int Coord1 =;
            int Coord2 =;
            newIcon->setGeometry( Coord1, Coord2, 75, 75 );
            out << QString("undoOneMove3-Function-End");
    std::stack<Drag3Widget*> undoMove3::undoStack3;
    std::stack< int > undoMove3::undoCoord3x;
    std::stack< int > undoMove3::undoCoord3y;


    #include "mainwindow.h"
    #include "dragwidget.h"
    #include "undomove.h"
    #include <QApplication>
    #include <QLabel>
    #include <QPushButton>
    #include <QLayout>
    #include <QFrame>
    #include <QHBoxLayout>
    #include <QDrag>
    #include <QMimeData>
    #include <QMouseEvent>
    #include <QToolButton>
    #include <QTextEdit>
    #include <QObject>
    #include <QTextStream>
    #include <QGlobal.h>
    #include <QTime>
     int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QWidget* hauptfenster = new QWidget();
        QPushButton* undo = new QPushButton (hauptfenster);
        undo-> setGeometry(1270,670,80,80);
        undo-> setIcon(QPixmap(":/images/retry.png"));
        undo-> setIconSize(QSize(80,80));
        undoMove3 undoMoveObject;
        QObject::connect(undo, &QPushButton::clicked, &undoMoveObject, 
        &undoMove3::undoOneMove3 );
        return a.exec();

  • Moderators

    @Katha99 said in Need help with signals and slots:

    if ( undoMove3::undoStack3.empty() )

    Are you sure the stack is empty? If not, your whole code won't be executed.

    out << QString("undoOneMove3-Function-Beginning");

    There is no need to wrap literals in QString() like this. Standard way of printing is to use qDebug() instead of text stream (but your solution should work).

  • I don't get the text I put in the function.

    99.9% chance it's just because undoMove3::undoStack3.empty() returns false. Use a debugger and put a breakpoint before the if is checked.

    Looks like you are using widgets to do stuff that should be done using the graphics view framework. Might be worth having a stab at redesigning the program from the ground up

  • @sierdzio, @VRonin
    Thank you very much.
    I dont know why I put that if clause there in the beginning anymore. But actually it doesn't make any sense.... I didn't even look at the if clause while looking for the error...

    I tried the code without the if clause and it worked.
    Thank you very much for your help.

    I will try using qDebug() next time and I will take a look at the Qt Graphics Framework. I don't know it, to be honest.

Log in to reply