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] Trying to add buttons, check boxes, etc. to a QTableWidget and get clicked signals using signalMapper

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k 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.
  • B Offline
    B Offline
    BasicPoke
    wrote on last edited by
    #1

    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
    0
    • B Offline
      B Offline
      BasicPoke
      wrote on last edited by
      #2

      In my QMainWindow constructor:

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • B Offline
          B Offline
          BasicPoke
          wrote on last edited by
          #4

          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
          0

          • Login

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