Results 1 to 3 of 3

Thread: QTableView will not display in a QTabWidget

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Angry QTableView will not display in a QTabWidget

    I am pretty new with Qt, and I am sure for an experienced programmer this problem is more than ridiculous. But I am eager to learn it. Hence, I am most interested to understand why my code is not working.
    Thanks to everybody, who brings me into the right direction to solve my problem!

    Okay, here is my problem:
    In an QApplication I have QTabWidget (NOT QTableWidget!) as a central Widget.
    The QTabWidget has three tabs.
    The content for each Tab should come from a "selfmade" class derived from QWidget.

    For the first tab "ra", it work as desired. But now I am stuck with the second tab "rf". This second tab should show a QTableView. I have marked the relevant parts in the code below with "//PROBLEM".

    mainwindow.cpp
    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include <QAction>
    3. #include <QMenuBar>
    4. #include "mainwindow.h"
    5. #include "Reiseangaben/reiseangaben.h"
    6. #include "Reiseabfolge/reiseabfolge.h"
    7. #include "Reisespesen/reisespesen.h"
    8.  
    9. MainWindow::MainWindow() {
    10.  
    11. setWindowTitle(tr("Alexander's Reisekostenabrechner"));
    12. resize(400, 200);
    13. createActions();
    14. createMenus();
    15.  
    16. widget_TabCentral = new QTabWidget(this);
    17. setCentralWidget(widget_TabCentral);
    18.  
    19. widget_TabCentral->setObjectName(QString::fromUtf8("widget_TabCentral"));
    20.  
    21. Reiseangaben *ra = new(Reiseangaben); // WORKS WELL
    22. widget_TabCentral->addTab(ra, "Allgemeine Reiseangaben");
    23.  
    24. Reiseabfolge *rf = new(Reiseabfolge); // PROBLEM
    25. widget_TabCentral->addTab(rf, "Reiseabfolge / Tagespauschale");
    26.  
    27. Reisespesen *sp = new(Reisespesen);
    28. widget_TabCentral->addTab(sp, "Spesen");
    29. }
    To copy to clipboard, switch view to plain text mode 


    To keep it simple for the beginning, I decide just to display in the second (Problem-)Tab rf a QTableView with content about the current directory (QDirModel). I have putted the QTableView into a QGridLayout. I am not sure if this is necessary. I did this to make sure my QTableView is within a widget, since addTab requires a widget as an argument.

    reiseabfolge.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "reiseabfolge.h"
    3.  
    4. Reiseabfolge::Reiseabfolge() {
    5.  
    6. QGridLayout *layoutGrid = new QGridLayout(this);
    7.  
    8. QDirModel dirModel;
    9.  
    10. QTableView *tav = new QTableView;
    11. tav->setModel(&dirModel);
    12. layoutGrid->addWidget(tav, 0, 0);
    13.  
    14. QModelIndex cwdIndex = dirModel.index(QDir::currentPath());
    15. tav->setRootIndex(cwdIndex);
    16. }
    To copy to clipboard, switch view to plain text mode 


    What is my problem:
    The second Tab shows only a white box with noting in it. The white box must be the QGridLayout, but this QGridLayout is empty. It does not show the QTableView .

    For completeness, here some further files:
    reiseabfolge.h
    Qt Code:
    1. #ifndef REISEABFOLGE_H
    2. #define REISEABFOLGE_H
    3.  
    4. #include <QtGui/QWidget>
    5.  
    6. class Reiseabfolge : public QWidget {
    7.  
    8. public:
    9. Reiseabfolge();
    10. };
    11.  
    12. #endif // REISEABFOLGE_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class MainWindow : public QMainWindow {
    7.  
    8. private:
    9. Q_OBJECT
    10. QTabWidget *widget_TabCentral;
    11. void createActions();
    12. void createMenus();
    13.  
    14. QMenu *dateiMenu;
    15. QAction *neueDatei_Action;
    16. QAction *oeffneDatei_Action;
    17. QAction *speicherDatei_Action;
    18. QAction *speicherDateiUnter_Action;
    19. QAction *beendeProgramm_Action;
    20.  
    21. public:
    22. MainWindow();
    23. ~MainWindow();
    24.  
    25. private slots:
    26. void neueDatei();
    27. void oeffneDatei();
    28. bool speicherDatei();
    29. bool speicherDateiUnter();
    30. bool beendeProgramm();
    31.  
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Thanks to everybody, who can tell me what I have misunderstood and made wrong.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView will not display in a QTabWidget

    You are setting a model that is a local variable. When the constructor ends, the object goes out of scope and is destroyed, hence your table view doesn't have a model assigned to it anymore. Create the model on the heap and all will be fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    CBM (7th June 2009)

  4. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView will not display in a QTabWidget

    Qt expects everything to be allocated on heap, at least QObjects

  5. The following user says thank you to nish for this useful post:

    CBM (7th June 2009)

Similar Threads

  1. Replies: 5
    Last Post: 25th May 2011, 10:10
  2. Replies: 1
    Last Post: 15th April 2009, 09:00
  3. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  4. QTableview data display font question
    By MarkoSan in forum Newbie
    Replies: 8
    Last Post: 14th May 2008, 12:57
  5. QTabWidget Placement and Display
    By mclark in forum Newbie
    Replies: 1
    Last Post: 16th January 2007, 20:34

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.