Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [solved] Trying to add buttons, check boxes, etc. to a QTableWidget and get clicked signals using signalMapper

    General and Desktop
    2
    4
    2536
    Loading More Posts
    • 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.
    • B
      BasicPoke last edited by

      QHello, I am trying to add buttons, check boxes, and the like, to a QTableWidget. When these controls are clicked, I want to get a signal and be able to tell which column of the table was clicked. I can't seem to get the clicked () signal to run.

      I am using the following for reference:
      http://qt-project.org/faq/answer/use_of_setcellwidget_in_qtablewidget_and_signal_connections
      http://qt-project.org/doc/qt-5/QSignalMapper.html

      Here is my code:

      @class ButtonWidget : public QWidget
      {
      Q_OBJECT
      public:
      ButtonWidget( QWidget *parent=0 );

      signals:
      void clicked( const int index );

      private:
      Ui::GaugeMon *ui;
      QSignalMapper *signalMapper;
      };

      ButtonWidget::ButtonWidget( QWidget *parent ) : QWidget(parent)
      {
      signalMapper = new QSignalMapper(this);
      for( int i=0; i<8; i++ )
      {
      QPushButton browseButton = new QPushButton("...", parent);
      connect( browseButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
      signalMapper->setMapping( browseButton, i );
      ((QTableWidget
      )parent)->setCellWidget(1, i, browseButton);
      }
      connect(signalMapper, SIGNAL(mapped(int)), this, SIGNAL(clicked(int)));
      }

      void ButtonWidget::clicked( const int index )
      {
      QMessageBox::critical(this, QString::number(index), "");
      }
      @

      The ButtonWidget::clicked signal does not run when I click the buttons. What am I missing here? I tried using the following line instead:
      @connect( browseButton, SIGNAL(clicked(const int)), signalMapper, SLOT(map()));@
      but then I get a message in the App Output, "QObject::connect: No such signal QPushButton::clicked(const int)..."

      Thanks
      Ron

      1 Reply Last reply Reply Quote 0
      • B
        BasicPoke last edited by

        In my QMainWindow constructor:

        @ButtonWidget b(ui->tableWidget); //Draw buttons on grid and make click connections.
        @

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Signals don't have implementation, only slots, so you just need to change that and update the connection call and you should be good to go

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • B
            BasicPoke last edited by

            I finally got this working using a slightly different method. My slot btnOutClickSlot(int btn) now runs when I click a button and btn contains the index of the button. I'm happy. :)

            @QSignalMapper *btnGroup;
            btnGroup = new QSignalMapper(this);
            connect(btnGroup, SIGNAL(mapped(int)), this, SLOT(btnOutClickSlot(int)));

            //Add buttons to table
            for( int i=0; i<8; i++ )
            {
            QPushButton *pb = new QPushButton("...", ui->tableWidget);
            btnGroup->setMapping( pb, i );
            connect( pb, SIGNAL(clicked()), btnGroup, SLOT(map()));
            ui->tableWidget->setCellWidget(1, i, pb);
            }

            void GaugeMon::btnOutClickSlot(int btn)
            {
            QMessageBox::critical(this, QString::number(btn), tr(""));
            }@

            1 Reply Last reply Reply Quote 0
            • First post
              Last post