Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use the corner button of QTableView?



  • Hi, all
    Click the button in the upper left corner of QTableView, the default function is to select the entire table. I want to display the "reset" text on this corner button and trigger a custom function after clicking it, such as clearing the QTableView. I did not find the signal of the corner button, and "setCornerWidget " cannot set another QPushButton correctly. Could you give me some suggestion please?
    Best regards!



  • Qt doesn't have API for that. But I think it may be done by cheating ways. :)
    And "setCornerWidget“ is a member function of QAbstractScrollArea, so it means the bottom right corner of the scroll bars.



  • @Bonnie When you said that, I found a small button in the bottom right corner. Thank you!
    "by cheating ways", is it to create a new QPushbutton covering the upper left corner please?



  • I mean using QObject::findChild / QObject::findChildren like

    //the top left corner button should be the only button that is the tableview's direct child
    //If there are multiple, you should look for whose class name is "QTableCornerButton"
    if(auto button = ui->tableWidget->findChild<QAbstractButton*>(QString(), Qt::FindDirectChildrenOnly)) {
        //this button is not a normal button, it doesn't paint text or icon
        //so it is not easy to show text on it, the simplest way is tooltip
        button->setToolTip("Reset");
        //disconnect the connected slots to the tableview (the "selectAll" slot)
        disconnect(button, Q_NULLPTR, ui->tableWidget, Q_NULLPTR);
        //connect "clear" slot to it, here I use QTableWidget's clear, you can connect your own
        connect(button, &QAbstractButton::clicked, ui->tableWidget, &QTableWidget::clear);
    }
    

    But covering it with another button also seems to be a workable approach, you can try that



  • @Bonnie Hi, Bonnie
    Your reply solved my question, thank you very much!
    I'm sorry I didn't get back to you in time.
    Best Regards!


Log in to reply