#include "mainwindow.h" #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QWidget *pWidget = new QWidget(this); //centra widget m_pTable = new QTableWidget(m_nRows,m_nCols); /* if you set ResizeMode to QHeaderView::ResizeToContents, it takes a long time to show the table. * It will be much more better if you use the other modes such as QHeaderView::Interactive, QHeaderView::Fixed or QHeaderView::Stretch. */ m_pTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); m_pShowBtn = new QPushButton(tr("show")); connect(m_pShowBtn,SIGNAL(pressed()),this,SLOT(ShowTableItems())); QHBoxLayout *pHbox = new QHBoxLayout; pHbox->addStretch(); pHbox->addWidget(m_pShowBtn); QVBoxLayout *pVbox = new QVBoxLayout; pVbox->addWidget(m_pTable); pVbox->addLayout(pHbox); pWidget->setLayout(pVbox); this->setCentralWidget(pWidget); } MainWindow::~MainWindow() { } void MainWindow::ShowTableItems() { m_pTable->clearContents(); for(int i = 0; i < m_nRows; i++) { for(int j = 0; j < m_nCols; j++) { m_pTable->setItem(i,j,new QTableWidgetItem("data_"+ QString::number(m_nCols*i+j+1))); } } }