Current row activation on QTableWidget with cell widgets



  • Hi,

    I have a QTableView where I add widgets in the cells: QLineEdit, QSpinBox, QPushButton and a Custom QLabel (I put a normal QLabel in my example). Here is the example:
    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include <QTableWidget>
    #include <QHeaderView>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QDoubleSpinBox>
    #include <QLabel>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QTableWidget* table = new QTableWidget();
        table->setSortingEnabled(false);
        table->setColumnCount(6);
    
        QHeaderView *headerView = table->horizontalHeader();
        headerView->setSectionResizeMode(0, QHeaderView::Stretch);
        headerView->setSectionResizeMode(1, QHeaderView::Fixed);
        headerView->setSectionResizeMode(2, QHeaderView::Fixed);
        headerView->setSectionResizeMode(3, QHeaderView::Fixed);
        headerView->setSectionResizeMode(4, QHeaderView::Fixed);
        headerView->setSectionResizeMode(5, QHeaderView::Fixed);
        headerView->resizeSection(1, 30);
        headerView->resizeSection(2, 70);
        headerView->resizeSection(3, 70);
        headerView->resizeSection(4, 30);
        headerView->resizeSection(5, 70);
    
        for(int n = 0; n < 10; n++)
        {
            int row = table->rowCount();
            table->insertRow(row);
            table->setCellWidget(row, 0, new QLineEdit);
            table->setCellWidget(row, 1, new QPushButton);
            table->setCellWidget(row, 2, new QDoubleSpinBox);
            table->setCellWidget(row, 3, new QDoubleSpinBox);
            table->setCellWidget(row, 4, new QPushButton);
            table->setCellWidget(row, 5, new QLabel);
        }
    
        setCentralWidget(table);
    }
    
    MainWindow::~MainWindow()
    {
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    };
    
    #endif // MAINWINDOW_H
    

    untitled.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-02-06T09:42:20
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = untitled
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h
    

    All the widgets handle mouse and key events as expected. So you can interact with them to enter text, change value, etc.

    What does not work as expected is that, let say, if I edit the text in the QLineEdit on row 3, I expect the row 3 to become the current/selected row. The only way to change the current row is to use the tab key, click the row number or click in this example on the QLabel which does not handle the mouse event unlike my custom QLabel does.

    Is there a better way to get the current row selected when editing any widgets other than subclassing them all, re-implement QWidget::focusInEvent and do the magics there?

    Thanks
    -Damien



  • You are needlessly abusing setCellWidget what you really need is a QStyledItemDelegate for columns 1-4 (if you are feeling lazy you can use this one but be aware it's VERY far from efficiency) and set the flags() on the model to return non-editable for column 5


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.