Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ArrowPad example: Crash while Signal trying to connect with slot



  • Good day everyone,

    For today I tried to play around with the arrowpad example in QT. So far, I attempt to move the whole dialog as a whole rather than the QPushButton in the dialog. I am not sure why it crashes even though I tried to match the signal and slot. Is there anything I miss out? Thank you!

    *Any other critique is welcomed as I really wanted to know a better way of coding or so. :)

    arrowpad.h

    #ifndef ARROWPAD_H
    #define ARROWPAD_H
    
    #include <QWidget>
    #include "mainwindow.h"
    
    QT_BEGIN_NAMESPACE
    class QPushButton;
    class MainWindow;
    QT_END_NAMESPACE
    
    
    class ArrowPad : public QWidget{
    
        Q_OBJECT
    
    public:
        ArrowPad(QWidget *parent = 0);
    
        QPushButton *bigLeftButton;
        QPushButton *bigRightButton;
        QPushButton *bigUpButton;
        QPushButton *bigDownButton;
    
    private:
        MainWindow *MainWindowMoving;
    
        QPushButton *upButton;
        QPushButton *downButton;
        QPushButton *leftButton;
        QPushButton *rightButton;
    
        //Add-on
        QPushButton *upDiagonalLeftButton;
        QPushButton *upDiagonalRightButton;
        QPushButton *downDiagonalLeftButton;
        QPushButton *downDiagonalRightButton;
        QPushButton *middleButton;
    
    private slots:
        void keyboardmovement();
    
    
    };
    
    #endif
    
    

    mainwindow.h

    //#ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "arrowpad.h"
    
    QT_BEGIN_NAMESPACE
    class QAction;
    class QMenu;
    class QPushButton;
    class ArrowPad;
    
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    
    {
        Q_OBJECT
    
    public:
        MainWindow();
    
    public slots:
        void windowMovement();
    
    private:
        ArrowPad *arrowPad;
        QMenu *fileMenu;
        QAction *exitAct;
        QPushButton *AupButton;
    
    };
    
    
    #endif
    
    

    arrowpad.cpp

    //#include <QtWidgets>
    
    #include "arrowpad.h"
    #include "mainwindow.h"
    
    ArrowPad::ArrowPad(QWidget *parent)
        : QWidget(parent)
    {
        MainWindowMoving = new MainWindow;
    
        upButton = new QPushButton(tr("&Up"));
        downButton = new QPushButton(tr("&Down"));
        leftButton = new QPushButton(tr("&Left"));
        rightButton = new QPushButton(tr("&Right"));
    
    
        upDiagonalLeftButton = new QPushButton(tr("&Up diagonal left"));
        upDiagonalRightButton = new QPushButton(tr("&Up diagonal right"));
        downDiagonalLeftButton = new QPushButton(tr("&down diagonal left"));
        downDiagonalRightButton = new QPushButton(tr("&down diagonal right"));
        middleButton = new QPushButton(tr("&Reset"));
        //Add on
        bigLeftButton = new QPushButton(tr("&Big Left"));
        bigRightButton = new QPushButton(tr("&Big Right"));
        bigUpButton = new QPushButton(tr("&Big Up"));
        bigDownButton = new QPushButton(tr("&Big Down"));
    
        QGridLayout *mainLayout = new QGridLayout;
    
        mainLayout->addWidget(upButton, 1, 2);
        mainLayout->addWidget(leftButton, 2, 1);
        mainLayout->addWidget(rightButton, 2, 3);
        mainLayout->addWidget(downButton, 3, 2);
    
        //Add on
        mainLayout->addWidget(upDiagonalLeftButton, 1, 1);
        mainLayout->addWidget(upDiagonalRightButton, 1, 3);
        mainLayout->addWidget(downDiagonalLeftButton, 3, 1);
        mainLayout->addWidget(downDiagonalRightButton, 3, 3);
        mainLayout->addWidget(middleButton,2,2);
    
        //Add on for mainWindow
        mainLayout->addWidget(bigLeftButton,2, 0);
        mainLayout->addWidget(bigRightButton,2, 4);
        mainLayout->addWidget(bigUpButton,0, 2);
        mainLayout->addWidget(bigDownButton,4, 2);
    
        setLayout(mainLayout);
    
        //Add on : Setting of Signals and slots
        connect(upButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(leftButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(rightButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(downButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(upDiagonalLeftButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(upDiagonalRightButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(downDiagonalLeftButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(downDiagonalRightButton, SIGNAL(pressed()), this, SLOT(keyboardmovement()));
        connect(middleButton, SIGNAL(released()), this, SLOT(keyboardmovement()));
    
        middleButton->setCheckable(false);
        bigLeftButton->setCheckable(false);
    }
    
        //Add on : Movement of QPushButton inside dialog
     void ArrowPad::keyboardmovement()
     {
        int InitialX, InitialY;
        int MoveX, MoveY;
    
        MoveX = x();
        MoveY = y();
    
        if (middleButton->isChecked()== false)
        {
           InitialX = MoveX ;
           InitialY = MoveY ;
    
            middleButton->setChecked(true);
        }
    
         QPushButton * button = (QPushButton*)sender();
         if (button->text() == "&Up"){
           ArrowPad::move(MoveX,MoveY-10);
         }
         else if (button->text() == "&Down"){
             ArrowPad::move(MoveX,MoveY+10);
         }
         else if (button->text() == "&Left"){
             ArrowPad::move(MoveX-10,MoveY);
         }
         else if (button->text() == "&Right"){
             ArrowPad::move(MoveX+10,MoveY);
         }
         else if (button->text() == "&Up diagonal left"){
           ArrowPad::move(MoveX-10,MoveY-10);
         }
         else if (button->text() == "&Up diagonal right"){
             ArrowPad::move(MoveX+10,MoveY-10);
         }
         else if (button->text() == "&down diagonal left"){
             ArrowPad::move(MoveX-10,MoveY+10);
         }
         else if (button->text() == "&down diagonal right"){
             ArrowPad::move(MoveX+10,MoveY+10);
         }
         else if (button->text() == "&Reset"){
             ArrowPad::move(0, 20);
     //      ArrowPad::move(InitialX, InitialY);
             middleButton->setChecked(false);
         }
     }
    
    

    main.cpp

    //
    #include <QtWidgets>
    
    #include "mainwindow.h"
    
    //! [0]
    int main(int argc, char *argv[])
    //! [0] //! [1]
    {
        QApplication app(argc, argv);
    
        MainWindow mainWindow;
        mainWindow.resize(300,300);
        mainWindow.show();
        return app.exec();
    }
    
    

    mainwindow.cpp

    #include <QtWidgets>
    #include <QMainWindow>
    
    #include "arrowpad.h"
    #include "mainwindow.h"
    
    QT_BEGIN_NAMESPACE
    class QMainWindow;
    class QWidget;
    QT_END_NAMESPACE
    
    MainWindow::MainWindow()
    {
        arrowPad = new ArrowPad;
    
        setCentralWidget(arrowPad);
    
        exitAct = new QAction(tr("E&xit"), this);
        exitAct->setShortcuts(QKeySequence::Quit);
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    
        fileMenu = menuBar()->addMenu(tr("&File"));
        fileMenu->addAction(exitAct);
    
        connect(arrowPad->bigLeftButton, SIGNAL(released()), this, SLOT(windowMovement()));
    
    }
    
    void MainWindow::windowMovement(){                            // This is for mainwindow
    
        arrowPad = new ArrowPad;
    
        int DiaX = 100, DiaY = 200, DiaLength = 200, DiaWidth = 200;
        MainWindow::setGeometry(DiaX,DiaY,DiaLength,DiaWidth);
    
        arrowPad->bigLeftButton->setCheckable(false);
    
        int InitialX, InitialY;
    
        if (arrowPad->bigLeftButton->isChecked()== false){
           InitialX = DiaX ;
           InitialY = DiaY ;
    
             arrowPad->bigLeftButton->setCheckable(true);
        }
          QPushButton * button2 = (QPushButton*)sender();
          if (button2->text() == "&Big Left"){
            MainWindow::setGeometry(DiaX-100,DiaY,DiaLength,DiaWidth);
          }
    
    }
    
    


  • New update:

    Apparently after posting I went to go to arrowpad.cpp to remove Line 9: MainWindowMoving = new MainWindow;

    Thanks god it works but strangely it went too far on the left.

    Critique on the coding OR any suggestion to move left at a short step is welcomed :)


Log in to reply