How to Use QTableWidget: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Developing with Qt::General]] | [[Category:Developing with Qt::General]] | ||
[[Category:HowTo]] | |||
[[Category:Snippets]] | |||
[[Category:Tutorial]] | |||
[toc align_right= | [toc align_right="yes" depth="3"] | ||
'''English''' [[How_to_Use_QTableWidget_Bulgarian|Български]] | '''English''' [[How_to_Use_QTableWidget_Bulgarian|Български]] | ||
Line 9: | Line 12: | ||
== QTableWidget Overview == | == QTableWidget Overview == | ||
Using | Using "QTableWidget":http://doc.qt.io/qt-5.0/qtwidgets/qtablewidget.html developers can embed tables inside Qt applications. QTableWidget inherits "QTableView":http://doc.qt.io/qt-5.0/qtwidgets/qtableview.html. Items in a QTableWidget instance are provided by class "QTableWidgetItem":http://doc.qt.io/qt-5.0/qtwidgets/qtablewidgetitem.html. | ||
== Basic Usage == | == Basic Usage == | ||
Line 15: | Line 18: | ||
==== Set number of rows and columns ==== | ==== Set number of rows and columns ==== | ||
<code>m_pTableWidget- | <code>m_pTableWidget->setRowCount(10); | ||
m_pTableWidget->setColumnCount(3);</code> | |||
==== Insert labels into the horizontal header ==== | ==== Insert labels into the horizontal header ==== | ||
<code>m_TableHeader | <code>m_TableHeader<<"#"<<"Name"<<"Text"; | ||
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);</code> | |||
==== Insert data ==== | ==== Insert data ==== | ||
The simplest way to insert text into a cell: | The simplest way to insert text into a cell: | ||
<code>m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello"));<code> | |||
==== Hide vertical header aka the line counter ==== | ==== Hide vertical header aka the line counter ==== | ||
</code>m_pTableWidget- | </code>m_pTableWidget->verticalHeader()->setVisible(false);</code> | ||
h4. Hide grid | |||
<code>m_pTableWidget->setShowGrid(false);<code> | |||
==== Set background of the selected items ==== | ==== Set background of the selected items ==== | ||
</code>m_pTableWidget- | </code>m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}");</code> | ||
==== Disable editing ==== | ==== Disable editing ==== | ||
<code>m_pTableWidget- | <code>m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);<code> | ||
==== Selection mode and behavior ==== | ==== Selection mode and behavior ==== | ||
The behavior of the table for selecting rows and cells can be customized using methods | The behavior of the table for selecting rows and cells can be customized using methods "setSelectionBehavior":http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionBehavior-enum and "setSelectionMode":http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionMode-enum. The following example allows only single selection of a row: | ||
</code>m_pTableWidget- | </code>m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); | ||
m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);<code> | |||
==== Handling signals ==== | ==== Handling signals ==== | ||
QTableWidget provides appropriate | QTableWidget provides appropriate "signals for each event such as change of selection, click, double click, etc. ":http://doc.qt.nokia.com/latest/qtablewidget.html#signals Example of handling double click of a cell: | ||
</code>connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), this, SLOT( cellSelected( int, int ) ) );</code> | </code>connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), this, SLOT( cellSelected( int, int ) ) );</code> | ||
Line 57: | Line 66: | ||
* mainwindow.h | * mainwindow.h | ||
<code>#include | <code>#include <QTableWidget><code> | ||
</code> | </code> | ||
private slots: | |||
void cellSelected(int nRow, int nCol); | void cellSelected(int nRow, int nCol); | ||
Line 67: | Line 77: | ||
QTableWidget* m_pTableWidget; | QTableWidget* m_pTableWidget; | ||
QStringList m_TableHeader; | QStringList m_TableHeader; | ||
<code> | |||
* mainwindow.cpp | * mainwindow.cpp | ||
</code>< | </code> | ||
#include "mainwindow.h" | |||
#include <QApplication> | |||
#include <QDesktopWidget> | |||
#include <QtCore/QCoreApplication> | |||
#include <QHeaderView> | |||
#include <QMessageBox> | |||
MainWindow::MainWindow(QWidget *parent) | |||
: QMainWindow(parent), | |||
m_pTableWidget(NULL) | |||
{ | |||
m_pTableWidget = new QTableWidget(this); | |||
m_pTableWidget->setRowCount(10); | |||
m_pTableWidget->setColumnCount(3); | |||
m_TableHeader<<"#"<<"Name"<<"Text"; | |||
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader); | |||
m_pTableWidget->verticalHeader()->setVisible(false); | |||
m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); | |||
m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); | |||
m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection); | |||
m_pTableWidget->setShowGrid(false); | |||
m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}"); | |||
m_pTableWidget->setGeometry(QApplication::desktop()->screenGeometry()); | |||
//insert data | |||
m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello")); | |||
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), | connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), | ||
this, SLOT( cellSelected( int, int ) ) ); | |||
} | |||
MainWindow::~MainWindow() | MainWindow::~MainWindow() | ||
{ | |||
} | |||
void MainWindow::cellSelected(int nRow, int nCol) | void MainWindow::cellSelected(int nRow, int nCol) | ||
{ | |||
QMessageBox::information(this, "", | |||
"Cell at row "''QString::number(nRow)'' | |||
" column "''QString::number(nCol)'' | |||
" was double clicked."); | |||
} | |||
<code> | |||
== See Also == | == See Also == | ||
"How to use QTableWidget in Qt":http://www.developer.nokia.com/Community/Wiki/index.php?title=How_to_use_QTableWidget_in_Qt |
Revision as of 08:38, 25 February 2015
[toc align_right="yes" depth="3"]
English Български
How to Use QTableWidget
QTableWidget Overview
Using "QTableWidget":http://doc.qt.io/qt-5.0/qtwidgets/qtablewidget.html developers can embed tables inside Qt applications. QTableWidget inherits "QTableView":http://doc.qt.io/qt-5.0/qtwidgets/qtableview.html. Items in a QTableWidget instance are provided by class "QTableWidgetItem":http://doc.qt.io/qt-5.0/qtwidgets/qtablewidgetitem.html.
Basic Usage
Set number of rows and columns
m_pTableWidget->setRowCount(10);
m_pTableWidget->setColumnCount(3);
Insert labels into the horizontal header
m_TableHeader<<"#"<<"Name"<<"Text";
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
Insert data
The simplest way to insert text into a cell:
m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello"));<code>
==== Hide vertical header aka the line counter ====
m_pTableWidget->verticalHeader()->setVisible(false);
h4. Hide grid
m_pTableWidget->setShowGrid(false);<code>
==== Set background of the selected items ====
m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}");
Disable editing
m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);<code>
==== Selection mode and behavior ====
The behavior of the table for selecting rows and cells can be customized using methods "setSelectionBehavior":http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionBehavior-enum and "setSelectionMode":http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionMode-enum. The following example allows only single selection of a row:
m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
==== Handling signals ====
QTableWidget provides appropriate "signals for each event such as change of selection, click, double click, etc. ":http://doc.qt.nokia.com/latest/qtablewidget.html#signals Example of handling double click of a cell:
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), this, SLOT( cellSelected( int, int ) ) );
Example
The following code snippet uses QTableWidget and all described cases above. It has been tested on Symbian^3 device.
- mainwindow.h
#include <QTableWidget><code>
private slots:
void cellSelected(int nRow, int nCol);
private:
QTableWidget* m_pTableWidget;
QStringList m_TableHeader;
* mainwindow.cpp
- include "mainwindow.h"
- include <QApplication>
- include <QDesktopWidget>
- include <QtCore/QCoreApplication>
- include <QHeaderView>
- include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), m_pTableWidget(NULL)
{
m_pTableWidget = new QTableWidget(this); m_pTableWidget->setRowCount(10); m_pTableWidget->setColumnCount(3); m_TableHeader<<"#"<<"Name"<<"Text"; m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader); m_pTableWidget->verticalHeader()->setVisible(false); m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection); m_pTableWidget->setShowGrid(false); m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}"); m_pTableWidget->setGeometry(QApplication::desktop()->screenGeometry());
//insert data m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello"));
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ),
this, SLOT( cellSelected( int, int ) ) );
}
MainWindow::~MainWindow() { }
void MainWindow::cellSelected(int nRow, int nCol) {
QMessageBox::information(this, "", "Cell at row "QString::number(nRow) " column "QString::number(nCol) " was double clicked.");
}
See Also
"How to use QTableWidget in Qt":http://www.developer.nokia.com/Community/Wiki/index.php?title=How_to_use_QTableWidget_in_Qt