PDA

View Full Version : QTableView refresh



eleanor
6th October 2007, 13:05
Hi.

I created a QTableView which look like this: http://doc.trolltech.com/4.3/sql-relationaltablemodel.html

But the problem is when I add a row (I use QDialog for that).

So, a QDialog pops up and I fill out all the required fields and then I press ok (to save all the data to mysql table and close the QDialog)

What I want to do now is: when this QDialog window is closed I want to refresh the QTableView, so it will reflect the currently added row.

Now I have to reopen the program (reconnect to the database/table) in order to do that.

Is there an option for that?

marcel
6th October 2007, 13:28
Try calling model->select again. I am not sure if it works.

But why do you do it like this? You could add another row in the model and let the model submit the data. You shouldn't insert it manually. Then why use a model anyway? Just for reading data?

jpn
6th October 2007, 13:29
Hi eleanor :)

Try QSqlTableModel::select()

eleanor
6th October 2007, 14:01
Hi,it's me again.

Thanks for the advices, it works.

Now, I need another advice:

1 ) this is the declaration of a public slot
void add_changes(QString table_name);

2) I call it like this
connect(ok_button,SIGNAL(clicked()),this,SLOT(add_ changes(table)));
- the table = "town";

3) the error is:
Object::connect: No such slot dbWindow::add_changes(table)

What's wring.



And I have another question:
- how can I sort this dropdown menu alphanumerically: http://doc.trolltech.com/4.3/images/relationaltable.png

marcel
6th October 2007, 14:34
Now, I need another advice:

1 ) this is the declaration of a public slot
void add_changes(QString table_name);

2) I call it like this
connect(ok_button,SIGNAL(clicked()),this,SLOT(add_ changes(table)));
- the table = "town";

3) the error is:
Object::connect: No such slot dbWindow::add_changes(table)

What's wring.



You can't pass a parameter to a slot like that.
To make this work it really depends on what you need to do.
Do you have multiple buttons, one for each table?
You have to do a mapping from buttons to table names. You can use a QSignalMapper or use sender() in the slot to identify the button and use the correct table name. or you could create multiple slots.



And I have another question:
- how can I sort this dropdown menu alphanumerically: http://doc.trolltech.com/4.3/images/relationaltable.png

That's the QSqlRelationDelegate. I'm afreaid you can't do much about it.

eleanor
6th October 2007, 14:48
I don't have buttons for each table. Can you tell me how to do it with multiple slots?

jpn
6th October 2007, 16:19
You could also declare the slot with a default parameter like this:

void add_changes(QString table_name = "town");
Then you'd establish the connection without a parameter:

connect(ok_button,SIGNAL(clicked()),this,SLOT(add_ changes()));
Whenever the button is clicked, the default parameter "town" will be used. If you want to call the function with another parameter in code, just pass so:

add_changes("foo");

And I have another question:
- how can I sort this dropdown menu alphanumerically: http://doc.trolltech.com/4.3/images/relationaltable.png
I guess you would have to subclass QSqlRelationalDelegate and reimplement setEditorData():


void MySqlRelationalDelegate::setEditorData(QWidget* editor, const QModelIndex & index) const
{
QSqlRelationalDelegate::setEditorData(editor, index);
if (QComboBox* combo = dynamic_cast<QComboBox*>(editor))
{
QSortFilterProxyModel* proxy = new QSortFilterProxyModel(combo);
proxy->setSourceModel(combo->model());
// combo's current model must be reparented, otherwise QComboBox::setModel() will delete it
combo->model()->setParent(proxy);
combo->setModel(proxy);
combo->model()->sort(0);
}
}

eleanor
6th October 2007, 16:42
Thanks for the info.

Another question:

/* MENU */
QMenu *sifrant_meni = new QMenu();
sifrant_meni->addAction(tr("Kraj"),this,SLOT(open_window()));
sifrant_meni->addAction(tr("Podjetje"),this,SLOT(open_window()));
sifrant_meni->addAction(tr("Zapri"),this,SLOT(close()));

QMenuBar *top_meni = new QMenuBar(this);
top_meni->addMenu(tr("Sifrant"));
top_meni->addMenu(sifrant_meni);



When i compile this code the Sifrant QMenuBar shows up fine, but when I click it there are no actions, why?

eleanor
6th October 2007, 17:41
I sloved it:

//meni
QMenu *sifrant_meni = new QMenu(this);
sifrant_meni->addAction(tr("Kraj"),this,SLOT(open_window()));
sifrant_meni->addAction(tr("Podjetje"),this,SLOT(open_window()));
sifrant_meni->addAction(tr("Zapri"),this,SLOT(close()));
sifrant_meni->setTitle(tr("Sifrant"));

QMenuBar *top_meni = new QMenuBar(this);
top_meni->addMenu(sifrant_meni);

jpn
6th October 2007, 18:00
Another question:
Could you start a new thread with a meaningful title next time. :) I mean, don't ask unrelated questions in same thread, please.