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]Qpushbutton and clicked slot.
QtWS25 Last Chance

[SOLVED]Qpushbutton and clicked slot.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.0k Views
  • 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.
  • M Offline
    M Offline
    mehdi.nine
    wrote on last edited by
    #1

    Hi, i have a verticalLayout that there's three labels with a button in each line. when i click on each button the event
    @
    button_clicked()
    {

    }
    @
    is firing. this event is for all button i.e when i click on each of button this function is called. now i want when i click on each button the same row that button is in that remove. how can i do that? first of all must be a unique value that i detect which button is clicked and second i must delete that row but i know how.
    any idea?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      if you don't use threads and have a fixed number of buttons you can use the sender() method:
      @
      button_clicked()
      {
      if( sender() == myButton1 )
      {
      ....
      }
      }
      @
      But a more elegant way is using "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html class.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mehdi.nine
        wrote on last edited by
        #3

        thanks for your reply. what is myButton1 ? is the button name? but all of my buttons have same name. this is code:
        @
        QWidget *oneLineWidget = new QWidget(this);

            QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout();
            { //added these brackets just for the ease of reading.
                QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget);
                QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget);
                QPushButton *bFirst = new QPushButton(tr("first"), oneLineWidget);
                QPushButton *bSecond = new QPushButton(tr("delete"), oneLineWidget);
        
        
        
                oneLineWidgetLayout->addWidget(labFirst);
                oneLineWidgetLayout->addWidget(labSecond);
                oneLineWidgetLayout->addWidget(bFirst);
                oneLineWidgetLayout->addWidget(bSecond);
        
        
        
        
            }
            oneLineWidget->setLayout(oneLineWidgetLayout);
        
            ui->verticalLayout->addWidget(oneLineWidget);
        }
        ui->widget->setLayout(ui->verticalLayout);
        ui->scrollArea->setWidget(ui->widget);
        

        @
        when this function executed one line added.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          in this case go with the QSignalMapper, since you had to map the pushbutton object pointers to the row they are in to remove or at least have to do extra work to find the row they are in.

          Edit: sender() retruns a pointer. So you need to compare it to your pushbutton pointer.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mehdi.nine
            wrote on last edited by
            #5

            i do that like this:
            @
            signals:
            void click(int);

            private:
            Ui::MainWindow *ui;

            QSignalMapper *signalMapper;
            

            @
            and in .cpp :
            @
            void MainWindow::click(int x)
            {

            }
            @
            it's error is:
            not a slot or signal declaration.

            ok i solve this.many thanks of you.

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              your delare click(int) as signal but implement a function body for it (like a slot) ... this can't work.

              Here's how you do it:

              @
              //init signalsmapper (in constrcutor for example)
              signalMapper = new QSignalMapper(this);
              connect(signalMapper, SIGNAL(mapped(QWidget )), this SLOT(clicked(QWidget));
              @

              @
              /*

              • yout init as you already have it but with the property
                */
                QWidget *oneLineWidget = new QWidget(this);

                 QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout();
                 { //added these brackets just for the ease of reading.
                     QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget);
                     QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget);
                     QPushButton *bFirst = new QPushButton(tr("first"), oneLineWidget);
                     QPushButton *bSecond = new QPushButton(tr("delete"), oneLineWidget);
                      
                      signalMapper->setMapping ( bSecond, bSecond );
                
                
                     oneLineWidgetLayout->addWidget(labFirst);
                     oneLineWidgetLayout->addWidget(labSecond);
                     oneLineWidgetLayout->addWidget(bFirst);
                     oneLineWidgetLayout->addWidget(bSecond);
                
                
                
                
                 }
                 oneLineWidget->setLayout(oneLineWidgetLayout);
                
                 ui->verticalLayout->addWidget(oneLineWidget);
                

                }
                ui->widget->setLayout(ui->verticalLayout);
                ui->scrollArea->setWidget(ui->widget);
                @

              @
              //finally the clicked-Slot
              void clicked(QWidget* button)
              {
              //actually this method isn't that trivial, since the index of each button changes once a button is removed
              for (int i = 0; i < ui->verticalLayout->count(); i++)
              {
              if (QWidgetItem myItem = dynamic_cast <QWidgetItem>(ui->verticalLayout->itemAt(i)))
              {
              if( myItem->widget()->findChildren<QPushButton*>().contains(button) )
              {
              ui->verticalLayout->removeItem(myItem);
              myItem->widget()->deleteLayter();
              break;
              }
              }
              }

              }
              @

              THis should give you an idea how to solve it (not activly tested though).
              If you have any questions feel free to ask. ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              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