PDA

View Full Version : Question : place TableView in a tab



coolinstar
17th May 2012, 05:44
Hi all,

I am freshman of Qt programming. After setup the develop enviroment, I'm trying to layout something by pure coding but without Qt Designer in case to be familiar with Qt Libraries.

And now, I'm trying to place a table into a tab.

I can do this by a QTableWidget and QVBoxLayout, But it doesn't work by QTableView and QVBoxLayout....

I had tried for few days, but I still can't figure this out. So I want to raise up this question, is there any solution???

ChrisW67
17th May 2012, 08:27
"But it doesn't work," is not a useful description of a problem especially if you expect a useful answer to "is there any solution???". The answer is Yes.

Is there any chance you could show us what you have actually done? What, if any, are your compiler/linker/runtime error/warning messages? What is the on-screen result? What were you expecting? See this (http://www.catb.org/~esr/faqs/smart-questions.html#beprecise) for help.

A QTableWidget is-a QTableView, and both are QWidgets, so the two are interchangeable as far as layouts are concerned.

coolinstar
17th May 2012, 10:06
After try and error in this morning, now the tableview can successfully display in the Tab.
But application output section came some messages :



QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
...going on...


Here is the code. using Qt Creator IDE.
I made several tabs and try to place a table in the Tab named "TestTbTab"
In m_TabDialog.cpp, comment part is using TableWidget and can successfully build.
and the rest of the part shows my TableView usage.


m_TabDialog.h

#ifndef M_TABDIALOG_H
#define M_TABDIALOG_H

#include<QtGui>
#include<QDialog>
#include<QTableView>
#include<QTableWidget>
#include<QTableWidgetItem>
#include<QGridLayout>
#include"TableModel.h"

QT_BEGIN_NAMESPACE
class QDialogButtonBox;
class QFileInfo;
class QTabWidget;
QT_END_NAMESPACE

// class m a i n t a b
class MainTab : public QWidget
{
Q_OBJECT

public:
MainTab(const QFileInfo &fileInfo, QWidget *parent = 0);
};

// class D E B U G T A B
class DebugTab : public QWidget
{
Q_OBJECT

public:
DebugTab(const QFileInfo &fileInfo, QWidget *parent =0);
};

// class T e s t T a b
class TestTab : public QWidget
{
Q_OBJECT

public:
TestTab(const QFileInfo &fileInfo, QWidget *parent = 0);
};

//class T e s t T b T a b
class TestTbTab : public QWidget
{
public:
TestTbTab(const QFileInfo &fileInfo, QWidget *parent = 0);
QWidget *tab;
private:
QGridLayout *grid_layout;
TableModel *model;
QTableView *view;

};

//class T a b D i a l o g
class TabDialog : public QDialog
{
Q_OBJECT

public:
TabDialog(const QString &fileName, QWidget *parent = 0);

private:
QTabWidget *tabWidget;
QDialogButtonBox *btnBox;
QWidget tab;
};


#endif // M_TABDIALOG_H

m_TabDialog.cpp (for focusing on problem, pass other Tabs)



#include"m_TabDialog.h"

TabDialog::TabDialog(const QString &fileName, QWidget *parent) : QDialog(parent)
{
QFileInfo fileInfo(fileName);

tabWidget = new QTabWidget;
tabWidget->addTab(new MainTab(fileInfo),tr("Main"));
tabWidget->addTab(new DebugTab(fileInfo),tr("Debug"));
tabWidget->addTab(new TestTab(fileInfo),tr("Test"));
tabWidget->addTab(new TestTbTab(fileInfo),tr("testTable"));

btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel );

connect(btnBox, SIGNAL(accepted()),this,SLOT(accept()));
connect(btnBox, SIGNAL(accepted()),this,SLOT(reject()));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(tabWidget);
mainLayout->addWidget(btnBox);
setLayout(mainLayout);

setWindowTitle(tr("test yaya"));
}
/*
TestTbTab::TestTbTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent)
{
QTableWidget *tableWidget = new QTableWidget;
tableWidget->setRowCount(5);
tableWidget->setColumnCount(5);

tableWidget->setWindowTitle(QObject::tr("test tablewidget"));
tableWidget->maximumSize();

QStringList m_header;
m_header<<"item1"<<"item2";
tableWidget->setHorizontalHeaderLabels(m_header);

tableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
tableWidget->setItem(1,0,new QTableWidgetItem("Feb"));
tableWidget->setItem(2,0,new QTableWidgetItem("Feb"));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tableWidget);
setLayout(layout);
}*/

TestTbTab::TestTbTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent)
{
model = new TableModel(this);

grid_layout = new QGridLayout;
tab = new QWidget;
view = new QTableView(tab);
view->setModel(model);
view->setWindowTitle(QObject::tr("test designed Model"));
grid_layout->addWidget(view);
setLayout(grid_layout);
}

thanks for any advise / suggestion

ChrisW67
18th May 2012, 00:56
Good to see you fixed the original issue with the QTableView.

thanks for any advise / suggestion
You haven't described a new problem so I am not sure what advice you want.

Line 69 of your .h file, and line 53 of the .cpp file seem to be unneeded (member variable called tab) and will be a memory leak because you not allocate the object a parent at creation.

The output you refer to is not coming from this code. Probably a debugging statement inside the TableModel class I would guess. Not indicative of an error of any sort.