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

create checkboxes at run time



  • Hello, I have a QtableWidget and I when I receive a new data from Serial Port I create a new row in it with the data received. I want to create a new checkbox to QTableWidget's 4. column. The main purpose of checkboxes will be this: When pressed to a corresponding button, the row with the checkboxes checked will be transferred to another table. However, this method looks inefficient because then I need to create new checkboxes at run time as this: ( Because I do not know how many data I will receive from serial)

    QCheckBox *checkbox = new QCheckBoxthis);
    

    Then how will I be able to check if these checkboxes are checked or not? Since they don't have a name?
    So I am sure there are lots of better and easier methods for this purpose. What else I can use for that? Or how can I control the checkboxes created at runtime ?



  • @GunkutA
    Before you need to go in this direction: QTableWidgetItems have a checkstate, you can enable via Qt::ItemIsUserCheckable in flags. If you use that you won't have to mess with QCheckBox widgets.



  • @GunkutA
    Before you need to go in this direction: QTableWidgetItems have a checkstate, you can enable via Qt::ItemIsUserCheckable in flags. If you use that you won't have to mess with QCheckBox widgets.



  • @JonB Thanks. But I use CellDoubleClicked signals of the QTableWidget. So if a cell doubleclicked and user presses enter button, I send the data in the double clicked (selected) cell. So I believe I cannot use checkstate of the QTableWidgetItem. I believe they would interfere eachother.



  • @JonB Or actually I can select them with a button I believe.Thanks for the idea!



  • @GunkutA
    I don't claim to know about that. I still would be reluctant to put a checkbox widget on when there's an in-built, but let's assume that is what you want.

    There are several ways you can do this, and still address

    Then how will I be able to check if these checkboxes are checked or not? Since they don't have a name?

    For simplicity, let's assume you add the QCheckBox onto the cells (setCellWidget()). Then you can access that checkbox via QWidget *QTableWidget::cellWidget(int row, int column) const. qobject_cast() the widget to QCheckBox * to see if it's a checkbox. Is that what you had in mind?



  • @JonB Yes it was. But when there will be 100 rows. Checking all of them and creating 100 of checkboxes would be nonsense. I will go with your suggestion.


Log in to reply