PDA

View Full Version : Table isn't showed



8Observer8
5th March 2013, 14:43
Hello, Everyone

I don't see the content of the table. Why?

8787

mainwindow.cpp


#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>
#include <QMenu>
#include <QStatusBar>
#include <QTableView>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QSplitter>

#include <QStandardItemModel>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
createActions();
createMenus();
createToolBars();
statusBar()->showMessage(tr(""));

QTableView *table = new QTableView;

QSplitter splitter;
splitter.addWidget( table );

QStandardItemModel model(5, 2);
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );

if( c == 0 )
for( int i=0; i<3; i++ )
item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) );

model.setItem(r, c, item);
}

// model.setHorizontalHeaderItem( 0, new QStandardItem( "Name" ) );
// model.setHorizontalHeaderItem( 1, new QStandardItem( "Phone number" ) );

table->setModel( &model );
// table->setSelectionModel( table->selectionModel() );

QWidget *window = new QWidget;
setCentralWidget(window);

QLabel *matrixLabel = new QLabel(tr("Matrix:"));
QVBoxLayout *matrixLayout = new QVBoxLayout;
matrixLayout->addWidget(matrixLabel);
matrixLayout->addWidget(table);

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(matrixLayout);

window->setLayout(mainLayout);
}

void MainWindow::createActions()
{
loadAction = new QAction(QIcon(":/images/load.png"), tr("&Open"), this);
loadAction->setShortcut(tr("Ctrl+L"));
loadAction->setStatusTip(tr("Load a matrix"));
connect(loadAction, SIGNAL(triggered()), this, SLOT(loadSLOT()));

saveAction = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAction->setShortcut(tr("Ctrl+S"));
saveAction->setStatusTip(tr("Save a matrix"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(saveSLOT()));

saveAsAction = new QAction(QIcon(":/images/save_as.png"), tr("&Save As"), this);
saveAsAction->setShortcut(tr("Ctrl+Alt+S"));
saveAsAction->setStatusTip(tr("Save a matrix As..."));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAsSLOT()));
}

void MainWindow::createMenus()
{
QMenu *menu;

menu = menuBar()->addMenu(tr("&File"));
menu->addAction(loadAction);
menu->addAction(saveAction);
menu->addAction(saveAsAction);
}

void MainWindow::createToolBars()
{
QToolBar *toolbar;

toolbar = addToolBar(tr("File"));
toolbar->addAction(loadAction);
toolbar->addAction(saveAction);
toolbar->addAction(saveAsAction);
}

void MainWindow::loadSLOT()
{

}

void MainWindow::saveSLOT()
{

}

void MainWindow::saveAsSLOT()
{

}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QAction>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);

private slots:
void loadSLOT();
void saveSLOT();
void saveAsSLOT();

private:
void createActions();
void createMenus();
void createToolBars();

QAction *loadAction;
QAction *saveAction;
QAction *saveAsAction;
};

#endif // MAINWINDOW_H


main.cpp


#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow mw;
mw.resize(500, 300);
mw.show();

return app.exec();
}


MaxElement.pro


SOURCES += \
main.cpp \
mainwindow.cpp

HEADERS += \
mainwindow.h

RESOURCES += \
images.qrc


Thanks

Ashkan_s
5th March 2013, 15:01
Change line 30 of mainwindow.cpp to
QStandardItemModel *model = new QStandardItemModel(5, 2); and the other lines accordingly.

8Observer8
5th March 2013, 19:11
Thanks very much! You saved my life :)



QStandardItemModel *model = new QStandardItemModel(5, 2);




model->setItem(r, c, item);




table->setModel(model);