View Full Version : Ho two access ui of another widget?

13th December 2011, 09:49
I have main widget consisting of tableview and button.
When click on the button QDialog appears.
I put some data and execute query.When click update in QDialog i close() the dialog and want to update the table in the Widget.
If i wanted to update the dialog i would make like this

ui->this->tableView->repaint(); or call another method.

But how to reach and trigger the UI of another widget?From the dialog to update the table in the widget?

13th December 2011, 10:32
QDialog has a signal finished(int), emitted when the dialog closes, using the signal updated the table.

13th December 2011, 10:51
Ok, but i want to update the table not in the dialog but in the widget
The problem is not the signal but the slot and mainly how to trigger it.

13th December 2011, 11:04
I'm confused.

If you want to update a widget, you update the widget.
If you want to update your table, you connect a signal from some other source (ie your dialog) to your table and that's it.

If you asking how to get a pointer to your table to connect a signal then I say - you should have it.
If you don't or can't have it, create a slot in parent of the table that will know what to do.

If you don't want to use connect() then you can use QMetaObject:invokeMethod() but the principle is the same - you need to have an access to the object you want to connect/invoke method on.

13th December 2011, 18:35
i'm confused too.

In the dialog with name groupsadddialog.cpp , .h, .ui i have pushbutton and i in the cpp file i have the following:

void GroupsAddDialog::on_pushButton_add_clicked()
QSqlQuery query;
query.prepare("INSERT INTO customers (customer_id, name, address)"
"VALUES (?, ?, ?)");

i have widget with name groups.ui .cpp, .h and in this widget i have an object with name tableGroups

Please tell me what could you write in the source when i comment with //here in order to update/redraw the table.The method doesn't matter , i just want to know how you'll get access to the object of the widget from the dialog.

14th December 2011, 04:00
How is the table view connected to the content of the table? Are you using QSqlTableModel/QTableView or manually loading the data from the table into a QTableWidget? For the former you need to call QSqlTableModel::select() to requery the table, for the latter rerun your load function. You do this in a slot in your primary view. That slot is the target of a connection between a custom signal in your dialog and your primary view. At "//here" you emit that signal.

There are other ways to architect this.

14th December 2011, 10:47
i use QSqlTableModel and QTableView

Could you give me joker about other design patterns?

15th December 2011, 00:10
At the moment you are directly changing the table underneath the model. The model has no way to know this, so you have to manually cause the QSqlTableModel to re-read the table (which has the side-effect of invalidating selections, indexes etc). If your dialog is opened modal then you can do this when the exec() call returns and the user did not cancel the operation.

If your dialog used the model, and updated the values in the model using the setData() method, then the other views would automatically update to the new values. You still have to manage when this is committed to the database and be careful of invalidating model indexes you still need.

16th December 2011, 12:33
Ok, this approach sounds reasonable!!!
The problem with accessing the model remains.I mean i don't know how to do it.I f you want to spend 5 min to post some code i'll be very gratefull.
You can do it for example with QWidget with QButton, and QDialog that opens when the button is clicked.And when you click in the QDialog button to update Qlabel that belongs to QWidget for instance.
It's the easiest example i can figure out.

Than you in advance