Pass information from tableWidget to another window



  • Hi everyone,

    I use Qt/MySql.
    I created a window "table" where there is a table (TableWidget) that contains some people's information.
    I created a slot so that if we double click on a row, a second window "details" appears.
    My question is how to pass the information of the row that is double clicked to the window "details".

    Thanks.



  • The doubleClick() signal passes a QModelIndex, which contains the information needed (amongst others the row and the column).

    How you pass this information to your detail window is a question of taste. You can add an additional QModelIndex index or int row parameter to your constructor or have an explicit setter, which allows you to pass an index or a row.

    Be aware that if you want to store the index for further reference, you will have to use a QPersistentModelIndex, because the ordinary QModelIndex should always be treated as temporary.



  • If you create the 'details' window when the item in the TableWidget selected you can pass the required information to the details window in it's constructor.

    If you need two-way communication (for example if the user can change the data in the 'details' window and it have to pass it back to the TableWidget) the way I usually do this cross-window/object communication is do it with either a simple signal/slot connection or with a delegate object.

    With the signal-slot solution you can create signals in the 'details' window for every action the user can do in the details window (like the user clicked the OK button or the Cancel button) and after you create the details window you simple connect it's signals to slots in your 'parent' window (which contains the TableWidget).

    The delegate based solution is basically the same but instead of creation signals and slots for the communication you pass-in for the 'details' window an additional 'delegate' reference (a reference for the 'parent' window) and the details window will call the delegate's appropriate method(s) when it's needed.

    I would suggest to not to pass the clicked item's index (QModelIndex) but it's actual data to the 'details' window - separation is almost always a good thing (generally it's not good if 2 windows/object can modify each other's internal state/variables directly but rather via a specified interface).

    Both solutions have it's pros and cons like the signal-slot solution is (can be) easily thread-safe without any additional coding to provide thread-safe delegation but if thread safety is not a concern I prefer the delegate based solution because I personally find it as a cleaner solution.


Log in to reply
 

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