Results 1 to 6 of 6

Thread: QListWidget->QStackWidget->QTableView question

  1. #1
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QListWidget->QStackWidget->QTableView question

    Okay, I know I'm a babe in the woods with Qt and after a few days trying to solve this on the "Newbie" thread I thought I'd try again here...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...Should I quit trying to make this work and move on?

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListWidget->QStackWidget->QTableView question

    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...
    And without showing us at least that constructor code, how are we supposed to have even a clue about what you might be doing wrong?

  3. #3
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QListWidget->QStackWidget->QTableView question

    Well, in order to not waste anyone's time, I was asking a theoretical question more so than something specific to my code as I don't like asking people to code for me. I didn't think code was necessary but since it apparently is:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4.  
    5. {
    6. ui->setupUi(this);
    7.  
    8. //*******************************************************
    9. //* Retrieve data from the USER table for managing... *
    10. //*******************************************************
    11. usertablemodel = new QSqlTableModel(this);
    12.  
    13. usertablemodel->setTable ("user");
    14. usertablemodel->sort (0, Qt::AscendingOrder);
    15.  
    16. usertablemodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    17. usertablemodel->select ();
    18.  
    19. usertablemodel->setHeaderData (0, Qt::Horizontal,
    20. QObject::tr("Username"));
    21. usertablemodel->setHeaderData (1, Qt::Horizontal,
    22. QObject::tr("Password"));
    23. usertablemodel->setHeaderData (2, Qt::Horizontal,
    24. QObject::tr("Full Name"));
    25. usertablemodel->setHeaderData (3, Qt::Horizontal,
    26. QObject::tr("Status"));
    27. usertablemodel->setHeaderData (4, Qt::Horizontal,
    28. QObject::tr("Access Level"));
    29. usertablemodel->setHeaderData (5, Qt::Horizontal,
    30. QObject::tr("User Name"));
    31. usertablemodel->setHeaderData (6, Qt::Horizontal,
    32. QObject::tr("Activity Date"));
    33.  
    34. ui->ManageUsersTV->setModel (usertablemodel);
    35. ui->ManageUsersTV->resizeColumnsToContents ();
    36.  
    37. ui->ManageUsersTV->setEditTriggers (QAbstractItemView::AnyKeyPressed |
    38. QAbstractItemView::DoubleClicked);
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    I have a slot called on_AdminPB_clicked connected to a pushbutton (signal onclicked()) where the code above currently exists now, which works perfectly, but I just would prefer to have it in the constructor not the slot. I don't know what code to place in my slot in order to have the tableview display if the code is defined in the constructor vs. the slot.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListWidget->QStackWidget->QTableView question

    First thing that comes to mind is that if you are creating a table model in a slot every time the slot is called, then you have a built-in memory leak. As you have surmised, it is generally more correct to create the model (and the view) in the constructor, and simply update the model (and thus the view) in response to user actions.

    In my use of models, I declare them as stack-based member variables of the MainWindow: QAbstractTableModel userModel; (not * userModel). This ensures that the model gets properly deleted when the main window instance goes out of scope. In this case you call setModel( &userModel ) on the view.

    It isn't clear what you mean by "there is no table view to work with". Is there no table view at all (a blank space where the table view should be?), or do you mean that the table view is visible but not populated with data?

    If it is the former, then you either have the widget parenting incorrect, you have made the table invisible (in the designer maybe?), you haven't set a layout somewhere, or any nmber of things.

    If it is the latter, then the table probably isn't getting updated in the main window constructor because it isn't visible yet. In my use of table views with custom models, I have noticed that the model::data() method is only called for row and column indexes that are actually visible.

    As a first step, I would try moving the select() and resizeColumnsToContents() calls out of the constructor (leaving everything else there), and putting it in your push button onclicked() slot, assuming your tab widget and table are now properly visible on screen.

  5. #5
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QListWidget->QStackWidget->QTableView question

    Thanks for the reply -- I haven't been able to focus on my Qt project with some family issues going on so I apologize for not responding...

    I'm not sure why right now, but if I move the model/view creation to the constructor, then click on the button linked to my slot, the screen that appears is completely blank...No tableviews, no pushbuttons, nothing. If I move the exact code back to the slot, everything appears and I could do the data entry/editing as needed...So, in short, the former....I haven't had too much of a chance to investigate...

    I am worried about memory leaks but right now my father-in-law is putting me under the gun to just get *something* to his client that we can tweak later...That's grating to me because he's asking to make other shortcuts that I don't like (raw text data entry when I prefer comboboxes linked to lookup tables, for example). It's a fairly small project (for experts -- for newbies like me with a full time job, huge) so memory leaks probably won't have much of an impact, but they're still worrisome...

    Thanks for the advice -- I'll troubleshoot some on this this weekend!


    scott

  6. #6
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QListWidget->QStackWidget->QTableView question

    Just figured this out -- we were both wrong because I gave incomplete information and I just didn't make the connection in my brain...

    When my app first starts, the user is presented with a log in screen. This takes place in my on_LoginPB_clicked() slot, which is fired from a LOGIN pushbutton on a form that takes a username, password, and DB path...Since this is where the DB connection is created, defining my tableviews in the constructor can't work because it doesn't have a DB to operate against...

    If I put the code I posted earlier in the on_LoginPB_clicked() slot -- which is only ever called once -- after a successful log in, then it all works perfectly...

    As Bug Bunny puts it -- what a maroon...Sometimes it helps to just slow down and think logically about what's happening...

    thanks for the help!!!


    Scott

Similar Threads

  1. QTreeWidget and QStackWidget
    By Paulo Fernando Pimenta in forum Newbie
    Replies: 3
    Last Post: 1st March 2011, 18:27
  2. QListWidget Question
    By bbad68 in forum Newbie
    Replies: 11
    Last Post: 23rd January 2010, 08:52
  3. New member and a question about QListWidget
    By Pokepasa in forum Qt Programming
    Replies: 2
    Last Post: 4th November 2009, 10:24
  4. QListWidget question
    By rishid in forum Qt Programming
    Replies: 8
    Last Post: 18th January 2008, 13:50
  5. question regarding qlistwidget
    By amulya in forum Qt Programming
    Replies: 1
    Last Post: 6th October 2006, 07:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.