I use tableview to show data, but ??

  • Im trying to create a table where the user can edit the values in one column. The idea was that if the user double clicks ( or press and hold the mouse button) the value of one column turns into an edit field, where the value can be changed. If editing is finished (enter pressed or focus lost) the edit field should be hidden and the new value be shown. Also the new value should be sent to c++ by a signal.

    But i have problems how to realise this and where to start with it. I know how to handle the signals, doubleclick, pressAndHold events and all these stuff, but i have know idea how to combine it to get a table with the described behaviour :(

    Does anyone knows an example for this? Something that is near to my table.

  • Hi @Duy-Khang ,

    Typically you can have such a table by using QTableWidget. Check the link for a simple explanation on how to use it.

    QTableWidget is very easy to use, but it stores the data in the widget itself. If you have an already defined data structure that you want to show in a table widget, then you need to use QTableView widget associated with a QAbstractItemModel (which interfaces with your already existing data structure). This is explained in Model/View programming.

    An example of Model/View programming that is very easy to understand is the Editable Tree Model. This example works with a tree, not a table, but it is very similar to what you would do with a table.

    The Table Model Example is also a simple example of using Model/View programming and it uses a table, but it doesn't show how to create a model because it uses Qt's already available QSqlTableModel. It does show however how to create a table view that allows you to edit data.

    Note that you can easily open and run Qt's examples if you run QtCreator. You can simply click on "Examples" on the Welcome screen and select which example you would like to open.

  • but i want to use table view with QSqlQueryModel, how can i do it ?

  • The first lines of the documentation of QSqlQueryModel contain an example of how to show the results of a query in a table view.

Log in to reply