⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

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

  • 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:

    Here is my code:

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

    void clicked( const int index );

    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 );
    )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)..."


  • In my QMainWindow constructor:

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

  • Lifetime Qt Champion


    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

  • 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(""));

Log in to reply