Results 1 to 5 of 5

Thread: opening a QTableView window with a pushbutton

  1. #1
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default opening a QTableView window with a pushbutton

    Hi, I'm having some trouble trying to show a QTableView window with a push button.
    When I have the following in my main.cpp, it opens both the main window and the table window:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc,argv);
    4. //some splash screen stuff
    5. dcsTS w;
    6. w.show();
    7. //more splash screen stuff
    8.  
    9. QTableView tableView;
    10. RegModel RegModel(0);
    11. tableView.setModel(&RegModel);
    12. tableView.show();
    13.  
    14. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    Now, I created a function that in my main window file that activates a pushbutton, that when clicked should run those same four lines of code:
    Qt Code:
    1. void dcsTS::showTable(){
    2. QTableView tableView;
    3. RegModel RegModel(0);
    4. tableView.setModel(&RegModel);
    5. tableView.show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    instead of having it in the main file. I was hoping this would magically make the table pop up when i pressed the button, however this is not the case, any ideas?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: opening a QTableView window with a pushbutton

    Qt Code:
    1. void dcsTS::showTable(){
    2. QTableView tableView;
    3. RegModel RegModel(0);
    4. tableView.setModel(&RegModel);
    5. tableView.show();
    6. }// ... tableView and RegModel destroyed here
    To copy to clipboard, switch view to plain text mode 
    Your variables goes out of scope, create them on a heap ( operator new ) and / or keep as class members. Also, don't use class name as variable name, it's very confusing:
    Qt Code:
    1. class dcsTS ... {
    2. ...
    3. protected:
    4. QTableView * _view;
    5. RegModel * _model;
    6. ...
    7.  
    8. // dcsTS constructor:
    9. dcsTS::dcsTS( /*...*/ ){
    10. _view = new QTableView();
    11. _model = new RegModel(0);
    12. _view->setModel(_model);
    13. }
    14.  
    15. //
    16. void dcsTS::showTable(){
    17. _view->show();
    18. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to stampede for this useful post:

    willief (7th April 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opening a QTableView window with a pushbutton

    Glorious, I understand now and believe this is going to work, but there is one more small issue.
    When I declare the new constructor in the .h file, when I try to compile I get an error in my main file telling me that this call:
    Qt Code:
    1. dcsTS w;
    To copy to clipboard, switch view to plain text mode 

    is ambiguous between one of these two declarations in the header file:
    Qt Code:
    1. public:
    2. explicit dcsTS(QWidget *parent = 0);
    3. dcsTS();
    4. //there is also a destructor here
    To copy to clipboard, switch view to plain text mode 

    any ideas about how to get around this? thanks for all your help

    EDIT: the actual error says "error: call of overloaded 'dcsTS()' is ambigious".

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: opening a QTableView window with a pushbutton

    Don't add new constructor, put the code I've posted into existing one.
    About the error - it's because you have default value declared in first constructor, so compiler does not know what do you mean, should it call first one with parent=NULL, or second.
    Anyway, you don't need two constructors, just modify previous.
    Last edited by stampede; 7th April 2011 at 19:16. Reason: updated contents

  6. The following user says thank you to stampede for this useful post:

    willief (7th April 2011)

  7. #5
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opening a QTableView window with a pushbutton

    awesome, after solving some of my own little errors it worked out perfectly, thank you!

Similar Threads

  1. opening a second window
    By benlyboy in forum Newbie
    Replies: 3
    Last Post: 2nd March 2010, 04:08
  2. Resizing window depending on QTableView
    By Ferric in forum Newbie
    Replies: 2
    Last Post: 9th February 2010, 04:17
  3. Qt 4.6.0: Opening a dialog from a main window
    By dmginc in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2010, 12:16
  4. Opening a new window
    By k12yp70n in forum Newbie
    Replies: 1
    Last Post: 26th March 2009, 15:31
  5. How to resize window with pushbutton
    By kaydknight in forum Newbie
    Replies: 2
    Last Post: 13th January 2007, 12:17

Tags for this Thread

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.