From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


Matrix of button



  • Hi I've a problem and I need some hints.
    I've a Matrix of button and I want that when a button is pressed it return his position (row and col)

    QPushButton* Field[30][30];
    
    for(int i=0;i<30;i++)
    		for(int j=0;j<30;j++){
    			Field[i][j]=new QPushButton("", this);
    			Field[i][j]->setFixedHeight(23);
    			Field[i][j]->setFixedWidth(23);
    		}
    

    I want to do somethings like this(but working :))

    connect(Field[i][j], SIGNAL(clicked()), this, SLOT(printValues(i,j)));
    

    PS I know that I can't do this kind of connect, is just to give you an idea
    PPS I was looking for Signal Mapper but doesn't seems the right choice


  • Moderators

    @dijnr said in Matrix of button:

    I was looking for Signal Mapper but doesn't seems the right choice

    QSignalMapper is meant for exactly this kind of problem. Also, instead of an array you should put the buttons i a layout.

    QGridLayout* grid = new QGridLayout(someParent);
    QSignalMapper* mapper = new QSignalMapper(someParent);
    
    for(int i = 0; i < 30; ++i) {
        for(int j = 0; j < 30; ++j) {
            QPushButton* pb = new QPushButton(this); //If you need an empty string use QString() instead of "", but you don't need it here
            pb->setFixedSize(23, 23); //Don't need two separate calls
            mapper->setMapping(pb, i * 100 + j); //some easy encoding, you may use whatever you want
            connect(pb, &QPushButton::clicked, mapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
        }
    }
    

    Then just connect the mapper to some slot:

    connect(mapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), whatever, &SomeClass::someSlot);
    

    and decode the values in the slot:

    void SomeClass::someSlot(int value)
    {
        int row = value / 100;
        int col = value % 100;
        /* do something with row and col */
    }
    

  • Qt Champions 2018

    @dijnr Alternative is to use C++11 and Qt5 connect syntax:

    QPushButton* Field[30][30];
    
    for(int i=0;i<30;i++)
    		for(int j=0;j<30;j++){
    			Field[i][j]=new QPushButton("", this);
    			Field[i][j]->setFixedHeight(23);
    			Field[i][j]->setFixedWidth(23);
                            // You most probably will need this pointer to access you class member, so we capture it
                            connect(Field[i][j], QPushButton::clicked, [this,i,j]() { /*Do what ever you want with i and j*/ }));
    		}
    


  • Thank you, I've solved my problem



  • @Chris-Kawa thanks a lot,i just looking for the answer"why it must use the QT4's connect style to pass the compiler..."


  • Qt Champions 2018

    @shepher_whu I cannot see this question in this thread: ""why it must use the QT4's connect style to pass the compiler..."
    The old connect syntax uses macros (SIGNAL/SLOT), so the compiler does not check whether the signal and slot actually exists and have compatible parameters.
    Or do I misunderstand your question?


Log in to reply