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. Matrix of button
Forum Updated to NodeBB v4.3 + New Features

Matrix of button

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 3.1k Views 1 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.
  • dijnrD Offline
    dijnrD Offline
    dijnr
    wrote on last edited by
    #1

    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

    Chris KawaC jsulmJ 2 Replies Last reply
    0
    • dijnrD dijnr

      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

      Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      @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 */
      }
      
      S 1 Reply Last reply
      4
      • dijnrD dijnr

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by VRonin
        #3

        @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*/ }));
        		}
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • dijnrD Offline
          dijnrD Offline
          dijnr
          wrote on last edited by
          #4

          Thank you, I've solved my problem

          1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            @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 */
            }
            
            S Offline
            S Offline
            shepher_whu
            wrote on last edited by
            #5

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

            jsulmJ 1 Reply Last reply
            0
            • S shepher_whu

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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