Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to move a widget in a gridlayout
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to move a widget in a gridlayout

Scheduled Pinned Locked Moved General and Desktop
qwidgetqgridlayoutslotqpushbutton
7 Posts 4 Posters 7.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RDiGuida
    wrote on 8 May 2015, 14:07 last edited by RDiGuida 5 Nov 2015, 14:09
    #1

    Hello, I am trying to implement a simple 15 puzzle, and at the moment I am using a qGridLayout to lay my qPushButtons and I am trying to connect the function move() in order to move a button to a specified place when it is clicked. In order to do so I would like to pass the id of the button as a parameter of move() (I am using QButtonGroup as a container).

    #ifndef MOVINGPUZZLE_H
    #define MOVINGPUZZLE_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QWidget>
    #include <QApplication>
    #include <QLayout>
    #include <QButtonGroup>
    #include <QPushButton>
    #include <QString>
    #include <cmath>
    
    class MovingPuzzle : public QApplication
    {
        Q_OBJECT
        public:
            MovingPuzzle(int argc, char* argv[]);
            QMainWindow m_MainWindow;
            QGridLayout *myGrid;
            QButtonGroup *myGroup;
    
        public slots:
            void move(int id);
    };
    
    #endif // MOVINGPUZZLE_H
    

    and my implementation file

    
    #include "movingpuzzle.h"
    
    const int Num=16;
    
    MovingPuzzle::MovingPuzzle(int argc, char* argv[])
        :QApplication(argc, argv)
    {
        QWidget *mainWid = new QWidget(&m_MainWindow);
    
        m_MainWindow.setCentralWidget(mainWid);
    
        myGrid = new QGridLayout;
    
        myGroup = new QButtonGroup;
        QPushButton *newBut;
    
        for(int row=0;row<sqrt(Num);row++)
            for(int col=0;col<sqrt(Num);col++)
            {
                QString id = QString::number((col+1)+(row*sqrt(Num)));
    
                if(((col+1)+(row*sqrt(Num)))<Num)
                {
                    newBut = new QPushButton(id);
    
                    myGroup->addButton(newBut,id.toInt());
    
                    myGrid->addWidget(newBut,row,col);
    
                    connect(myGroup->button(id.toInt()),SIGNAL(clicked()),this,SLOT(move(id.toInt())));
                }
            }
    
        mainWid->setLayout(myGrid);
    }
    
    void MovingPuzzle::move(int id)
    {
        myGrid->addWidget(myGroup->button(id),3,3);
    }
    

    The problem is that the program is giving me hard times passing the id as a parameter, hence the connection does not work. As you can see for the moment I am sending all the buttons to a specified place (3,3) but in the future I will implement it so that every button will be sent to the empty spot.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sneubert
      wrote on 8 May 2015, 14:12 last edited by
      #2

      Use QSignalMapper (http://doc.qt.io/qt-5/qsignalmapper.html) to pass the button id to your moveslot

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RDiGuida
        wrote on 8 May 2015, 15:01 last edited by
        #3

        So is it wrong to pass arguments through a slot?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on 8 May 2015, 15:36 last edited by mchinand 5 Aug 2015, 15:38
          #4

          Instead of connecting each buttons clicked signal to a slot. You can use the QButtonGroup's buttonClicked(int id) signal which includes the pressed button's id.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RDiGuida
            wrote on 9 May 2015, 13:10 last edited by
            #5

            I get that it is more efficient to proceed like you suggest but I can't figure out what is wrong with my implementation. Any ideas?

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rondog
              wrote on 9 May 2015, 15:54 last edited by
              #6

              The problem is here:

              ...SLOT(move(id.toInt())...

              The SIGNAL() and SLOT() macros expect the declaration of the parameters. You should have something like 'SLOT(move(const int))' in the connect line. The function would be called from the button using emit (something like 'emit clicked(123)' which would end up calling your slot as 'move(123)').

              The signal mapper sounds like the right idea.

              1 Reply Last reply
              1
              • R Offline
                R Offline
                RDiGuida
                wrote on 11 May 2015, 14:08 last edited by
                #7

                Thank you, I had a bit of a read around signal mappers and they seem like the perfect tool for what I need.

                1 Reply Last reply
                0

                1/7

                8 May 2015, 14:07

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved