Nominate our 2022 Qt Champions!

Signal & Slot problem occured by defining own slot

  • Hello,

    I have the following files with my own class derived by QWigets:
    @#include "qt_windows.h"
    #include <QApplication>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QTableWidget>
    #include <QCheckBox>
    #include <QObject>

    class window_main : public QWidget

    QVBoxLayout *layout;
    QTableWidget *table;

    window_main(int rows, int columns);

    public slots:
    void click_handler(const QModelIndex &index);

    @#include "main.h"
    #include "qt_windows.h"
    #include <QApplication>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QTableWidget>
    #include <QCheckBox>
    #include <QObject>

    int main(int argc, char** args)
    QApplication app(argc, args);

    window_main window(50, 7);

    return 0;

    window_main::window_main(int rows, int columns)
    layout = new QVBoxLayout(this);
    table = new QTableWidget(rows, columns);

    connect(this->table, SIGNAL(clicked(const QModelIndex &index)), this, SLOT(click_handler(const QModelIndex &index)));


    void window_main::click_handler(const QModelIndex &index)

    When I click on the table, nothing happens. Does anyone have a clue why?

  • Hi,
    You do not need to give the argument name, remove the index name in your connect.
    That might do the trick.
    Also, what does your output window says?? IT will notify you if an connection could not be made

  • Thank's a lot, that was it!

  • Another question:
    Now I try to get information about the clicked cell. What I tried so far is:
    void window_main::click_handler(const QModelIndex& index)
    int index_row = index.row(), index_column = index.column();
    QTableWidgetItem* item = this->table->item(index_row, index_column);
    if(item != NULL)
    QMessageBox *msgbox = new QMessageBox(this);
    QString title = "Item selected", text = item->text();
    msgbox->information(NULL, title, text, QMessageBox::Ok);
    Problem is, that [code]item[/code] is always NULL... why?

  • Ok, as I know now I have to create QTableWidgetItems first and then add them to QTableWidget... sounds logical, BUT that won't solve my problem, because I have several different widgets in my QTableWidget (checkboxes, buttons etc.), which I can't put in a QTableWidgetItem but instead directly via setCellWidget(QWidget*)..

    Now my question:
    How is it possible to get the content of a clicked cell (QTableWidget), when it's contents are placed via setCellWidget?
    Background is, that I want to detect if user clicks first cell in a row --> then I'd like to get the checkstate of every QCheckBox in every cell in that row... so I have to get the CheckBox by only having a pointer to a cell (QModelIndex), not to the checkboxes itsself...
    I need something like a method to get the child elements of a cell...

    Any clues how to solve this?

    Solved -- created Widgets for every cell and got a pointer to them via QTableWidget::cellWidget(row, column)

Log in to reply