PDA

View Full Version : QTableView will not display in a QTabWidget



CBM
7th June 2009, 10:08
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


#include <QtGui/QMainWindow>
#include <QAction>
#include <QMenuBar>
#include "mainwindow.h"
#include "Reiseangaben/reiseangaben.h"
#include "Reiseabfolge/reiseabfolge.h"
#include "Reisespesen/reisespesen.h"

MainWindow::MainWindow() {

setWindowTitle(tr("Alexander's Reisekostenabrechner"));
resize(400, 200);
createActions();
createMenus();

widget_TabCentral = new QTabWidget(this);
setCentralWidget(widget_TabCentral);

widget_TabCentral->setObjectName(QString::fromUtf8("widget_TabCentral"));

Reiseangaben *ra = new(Reiseangaben); // WORKS WELL
widget_TabCentral->addTab(ra, "Allgemeine Reiseangaben");

Reiseabfolge *rf = new(Reiseabfolge); // PROBLEM
widget_TabCentral->addTab(rf, "Reiseabfolge / Tagespauschale");

Reisespesen *sp = new(Reisespesen);
widget_TabCentral->addTab(sp, "Spesen");
}



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


#include <QtGui>
#include "reiseabfolge.h"

Reiseabfolge::Reiseabfolge() {

QGridLayout *layoutGrid = new QGridLayout(this);

QDirModel dirModel;

QTableView *tav = new QTableView;
tav->setModel(&dirModel);
layoutGrid->addWidget(tav, 0, 0);

QModelIndex cwdIndex = dirModel.index(QDir::currentPath());
tav->setRootIndex(cwdIndex);
}



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


#ifndef REISEABFOLGE_H
#define REISEABFOLGE_H

#include <QtGui/QWidget>

class Reiseabfolge : public QWidget {

public:
Reiseabfolge();
};

#endif // REISEABFOLGE_H


mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow {

private:
Q_OBJECT
QTabWidget *widget_TabCentral;
void createActions();
void createMenus();

QMenu *dateiMenu;
QAction *neueDatei_Action;
QAction *oeffneDatei_Action;
QAction *speicherDatei_Action;
QAction *speicherDateiUnter_Action;
QAction *beendeProgramm_Action;

public:
MainWindow();
~MainWindow();

private slots:
void neueDatei();
void oeffneDatei();
bool speicherDatei();
bool speicherDateiUnter();
bool beendeProgramm();

};

#endif // MAINWINDOW_H


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

wysota
7th June 2009, 10:37
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.

nish
7th June 2009, 10:47
Qt expects everything to be allocated on heap, at least QObjects:)