PDA

View Full Version : Listwidget->tabwidget->tableview signal to activate



scott_hollen
3rd March 2011, 19:26
Okay, I know I'm a babe in the woods with Qt, but I *am* figuring out things, but this latest issue is a headscratcher...I think it comes down to something simple...

I have a list widget consisting of 5 push buttons. These are connected to a stacked widget of 5 tab widgets. On one of these tab widgets (A User admin one) is a TableView populated by a QSqlTableModel. A clicked() signal on on a button connects to a show() slot for this widget; I also have a clicked() signal connected to a slot called MainWindow::On_UserPB_Clicked. This is where I need help.

If I create my QSqlTableModel in the On_UserPB_Clicked slot and feed it to my TableView, everything works perfectly. I can add to it and edit it, I can click on other push buttons in my list widget and come back to the User tabwidget and all is well. However, according to everything I'm reading, I really should be creating the model and populating the TableView in the MainWindow::MainWindow constructor, but when I do this, my tab widget still gets displayed correctly, but there is no TableView to work with, and I don't know how to get this to appear.

Is there another signal I need to be capturing? I tried to connect a clicked() signal to a show() and an update() slot for my TableView, but to no avail...

I'm saving space and not posting the code (plus it's actually working when in the slot), I just don't know how to get it to display if it's defined in the constructor...

Thanks in advance!

scott

wysota
6th March 2011, 08:55
How does your constructor look like?

scott_hollen
6th March 2011, 14:34
Here's the constuctor -- from my comment that begins "Retrieve" on down works if instead of here in the constructor it's coded in my slot for the buttonclicked() signal...Right now, if the code is in the constructor, I need some method to display the model...


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)

{
ui->setupUi(this);

//************************************************** *****
//* Retrieve data from the USER table for managing... *
//************************************************** *****
usertablemodel = new QSqlTableModel(this);

usertablemodel->setTable ("user");
usertablemodel->sort (0, Qt::AscendingOrder);

usertablemodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
usertablemodel->select ();

usertablemodel->setHeaderData (0, Qt::Horizontal,
QObject::tr("Username"));
usertablemodel->setHeaderData (1, Qt::Horizontal,
QObject::tr("Password"));
usertablemodel->setHeaderData (2, Qt::Horizontal,
QObject::tr("Full Name"));
usertablemodel->setHeaderData (3, Qt::Horizontal,
QObject::tr("Status"));
usertablemodel->setHeaderData (4, Qt::Horizontal,
QObject::tr("Access Level"));
usertablemodel->setHeaderData (5, Qt::Horizontal,
QObject::tr("User Name"));
usertablemodel->setHeaderData (6, Qt::Horizontal,
QObject::tr("Activity Date"));

ui->ManageUsersTV->setModel (usertablemodel);
ui->ManageUsersTV->resizeColumnsToContents ();

ui->ManageUsersTV->setEditTriggers (QAbstractItemView::AnyKeyPressed |
QAbstractItemView::DoubleClicked);

}

wysota
6th March 2011, 14:48
So what's wrong exactly? The model seems to be properly set on the view. Assuming of course ui->ManageUsersTV is the view we're talking about.

scott_hollen
6th March 2011, 15:36
Yep, that's the correct view

Well, I have a clicked() signal attached to a pushbutton, so I had this code in my slot for that signal and it displayed the tableview fine. I thought it would be a better design approach to have this code in the constuctor instead (but I could be wrong), so now, I don't know what to do in my slot to get the tableview to display.

Here is what my slot previously looked like (identical to the constructor) -- my question is if I put this in the constuctor instead of the slot, how do I get the tableview to display?


void MainWindow::on_AdminPB_clicked()
{
//************************************************** *****
//* Retrieve data from the USER table for managing... *
//************************************************** *****
usertablemodel = new QSqlTableModel(this);

usertablemodel->setTable ("user");
usertablemodel->sort (0, Qt::AscendingOrder);

usertablemodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
usertablemodel->select ();

usertablemodel->setHeaderData (0, Qt::Horizontal,
QObject::tr("Username"));
usertablemodel->setHeaderData (1, Qt::Horizontal,
QObject::tr("Password"));
usertablemodel->setHeaderData (2, Qt::Horizontal,
QObject::tr("Full Name"));
usertablemodel->setHeaderData (3, Qt::Horizontal,
QObject::tr("Status"));
usertablemodel->setHeaderData (4, Qt::Horizontal,
QObject::tr("Access Level"));
usertablemodel->setHeaderData (5, Qt::Horizontal,
QObject::tr("User Name"));
usertablemodel->setHeaderData (6, Qt::Horizontal,
QObject::tr("Activity Date"));

ui->ManageUsersTV->setModel (usertablemodel);
ui->ManageUsersTV->resizeColumnsToContents ();

ui->ManageUsersTV->setEditTriggers (QAbstractItemView::AnyKeyPressed |
QAbstractItemView::DoubleClicked);

}

wysota
6th March 2011, 16:10
I don't really see the problem. You show a widget by calling show() on it or on its parent. It doesn't matter where you setup the model. The only part that will not work is resizeColumnsToContents() as it needs the view to be fully initialized to fulfill your request. You can do it on showEvent() or you can change the resize mode of the header to ResizeToContents.

scott_hollen
6th March 2011, 16:43
I tried a show() but it didn't work, but I'll try it again...I appreciate your help -- glad to know I'm not crazy!! (well, the jury's still out on that one)


scott

wysota
6th March 2011, 18:08
Please explain what you mean by "didn't work".

scott_hollen
28th March 2011, 18:33
Sorry for the delay in responding - family issues took me off my project for a few days...

I resolved this yesterday -- turns out there was nothing wrong with my code, only where it was being executed...If I placed it in the constructor, then, when my slot was fired I would get blank tableview(s)...When the same code was placed *in* my slot, everything displayed fine. Well, when my app first starts up, there is a login screen for my DB connection, and that was taking place *after* the tableviews/models were being defined -- meaning, they had no database to operate on so therefore they had no data...When moved the login to before the construction of my tableviews/models, everything worked perfect...Tableviews need DB connections before they'll work right :)

Thanks for you help!


scott