How to Use QTableWidget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Cleanup)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''English''' [[How to Use QTableWidget Bulgarian|Български]]
{{LangSwitch}}
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
__NOTOC__
Using {{DocLink|QTableWidget}} developers can embed tables inside Qt applications. QTableWidget inherits {{DocLink|QTableView}}. Items in a QTableWidget instance are provided by class {{DocLink|QTableWidgetItem}}.


=How to Use QTableWidget=
== Basic Usage ==


==QTableWidget Overview==
==== Set number of rows and columns ====


Using [http://doc.qt.io/qt-5.0/qtwidgets/qtablewidget.html QTableWidget] ''[qt.io]'' developers can embed tables inside Qt applications. QTableWidget inherits [http://doc.qt.io/qt-5.0/qtwidgets/qtableview.html QTableView] ''[qt.io]''. Items in a QTableWidget instance are provided by class [http://doc.qt.io/qt-5.0/qtwidgets/qtablewidgetitem.html QTableWidgetItem] ''[qt.io]''.
<code>m_pTableWidget->setRowCount(10);
m_pTableWidget->setColumnCount(3);</code>


==Basic Usage==
==== Insert labels into the horizontal header ====


====Set number of rows and columns====
<code>m_TableHeader<<"#"<<"Name"<<"Text";
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);</code>


====Insert labels into the horizontal header====
==== Insert data ====


====Insert data====
The simplest way to insert text into a cell:
<code>m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello"));</code>


The simplest way to insert text into a cell:<br />
==== Hide vertical header aka the line counter ====


====Hide vertical header aka the line counter====
<code>m_pTableWidget->verticalHeader()->setVisible(false);</code>


====Hide grid====
==== Hide grid ====
<code>m_pTableWidget->setShowGrid(false);</code>


====Set background of the selected items====
==== Set background of the selected items ====


====Disable editing====
<code>m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}");</code>


====Selection mode and behavior====
==== Disable editing ====


The behavior of the table for selecting rows and cells can be customized using methods [http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionBehavior-enum setSelectionBehavior] ''[qt.io]'' and [http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionMode-enum setSelectionMode] ''[qt.io]''. The following example allows only single selection of a row:
<code>m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);</code>


====Handling signals====
==== Selection mode and behavior ====


QTableWidget provides appropriate [http://doc.qt.nokia.com/latest/qtablewidget.html#signals signals for each event such as change of selection, click, double click, etc.] ''[doc.qt.nokia.com]'' Example of handling double click of a cell:
The behavior of the table for selecting rows and cells can be customized using methods [http://doc.qt.io/qt-5/qabstractitemview.html#Public-Functions setSelectionBehavior] and [http://doc.qt.io/qt-5/qabstractitemview.html#Public-Functions setSelectionMode]. The following example allows only single selection of a row:


==Example==
<code>m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);</code>
 
==== Handling signals ====
 
QTableWidget provides appropriate [http://doc.qt.io/qt-5/qtablewidget.html#signals signals for each event such as change of selection, click, double click, etc. ] Example of handling double click of a cell:
 
<code>connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ), this, SLOT( cellSelected( int, int ) ) );</code>
 
== Example ==


The following code snippet uses QTableWidget and all described cases above. It has been tested on Symbian^3 device.
The following code snippet uses QTableWidget and all described cases above. It has been tested on Symbian^3 device.


* mainwindow.h
* mainwindow.h
<code>#include <QTableWidget></code>
<code>
private slots:
void cellSelected(int nRow, int nCol);
private:
QTableWidget* m_pTableWidget;
QStringList m_TableHeader;
</code>


* mainwindow.cpp
* mainwindow.cpp


==See Also==
<code>
#include "mainwindow.h"
 
#include <QApplication>
#include <QDesktopWidget>
#include <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"));


[http://www.developer.nokia.com/Community/Wiki/index.php?title=How_to_use_QTableWidget_in_Qt How to use QTableWidget in Qt] ''[developer.nokia.com]''<br />[http://doc.qt.nokia.com/latest/stylesheet-examples.html#customizing-qtableview Customizing QTableView] ''[doc.qt.nokia.com]''
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ),
this, SLOT( cellSelected( int, int ) ) );
}


===Categories:===
MainWindow::~MainWindow()
{
}


* [[:Category:Developing-with-Qt|Developing with Qt]]
void MainWindow::cellSelected(int nRow, int nCol)
** [[:Category:Developing-with-Qt::General|General]]
{
* [[:Category:HowTo|HowTo]]
QMessageBox::information(this, "",
* [[:Category:snippets|snippets]]
"Cell at row "''QString::number(nRow)''
* [[:Category:Tutorial|Tutorial]]
" column "''QString::number(nCol)''
" was double clicked.");
}
</code>

Latest revision as of 19:23, 27 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Using QTableWidget developers can embed tables inside Qt applications. QTableWidget inherits QTableView. Items in a QTableWidget instance are provided by class QTableWidgetItem.

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"));

Hide vertical header aka the line counter

m_pTableWidget->verticalHeader()->setVisible(false);

Hide grid

m_pTableWidget->setShowGrid(false);

Set background of the selected items

m_pTableWidget->setStyleSheet("QTableView {selection-background-color: red;}");

Disable editing

m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

Selection mode and behavior

The behavior of the table for selecting rows and cells can be customized using methods setSelectionBehavior and setSelectionMode. 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. 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>
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 <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.");
}