How to Use QTableWidget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[How to Use QTableWidget Bulgarian|Български]]
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]


=How to Use QTableWidget=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


==QTableWidget Overview==
'''English''' [[How_to_Use_QTableWidget_Bulgarian|Български]]


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]''.
= How to Use QTableWidget =


==Basic Usage==
== QTableWidget Overview ==


====Set number of rows and columns====
Using &quot;QTableWidget&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtablewidget.html developers can embed tables inside Qt applications. QTableWidget inherits &quot;QTableView&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtableview.html. Items in a QTableWidget instance are provided by class &quot;QTableWidgetItem&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtablewidgetitem.html.


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


====Insert data====
==== Set number of rows and columns ====


The simplest way to insert text into a cell:<br />
<code>m_pTableWidget-&gt;setRowCount(10);<br />m_pTableWidget-&gt;setColumnCount(3);</code>


====Hide vertical header aka the line counter====
==== Insert labels into the horizontal header ====


====Hide grid====
<code>m_TableHeader&amp;lt;&lt;&quot;#&quot;&lt;&lt;&quot;Name&amp;quot;&lt;&lt;&quot;Text&amp;quot;;<br />m_pTableWidget-&gt;setHorizontalHeaderLabels(m_TableHeader);</code>


====Set background of the selected items====
==== Insert data ====


====Disable editing====
The simplest way to insert text into a cell:<br /><code>m_pTableWidget-&gt;setItem(0, 1, new QTableWidgetItem(&quot;Hello&amp;quot;));<code>


====Selection mode and behavior====
==== Hide vertical header aka the line counter ====


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-&gt;verticalHeader()<s>&gt;setVisible(false);</code>
<br />h4. Hide grid
<br /><code>m_pTableWidget</s>&gt;setShowGrid(false);<code>


====Handling signals====
==== Set background of the selected items ====


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:
</code>m_pTableWidget-&gt;setStyleSheet(&quot;QTableView {selection-background-color: red;}&quot;);</code>


==Example==
==== Disable editing ====
 
<code>m_pTableWidget-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);<code>
 
==== Selection mode and behavior ====
 
The behavior of the table for selecting rows and cells can be customized using methods &quot;setSelectionBehavior&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionBehavior-enum and &quot;setSelectionMode&amp;quot;: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-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);<br />m_pTableWidget-&gt;setSelectionMode(QAbstractItemView::SingleSelection);<code>
 
==== Handling signals ====
 
QTableWidget provides appropriate &quot;signals for each event such as change of selection, click, double click, etc. &quot;: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>
 
== 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 &lt;QTableWidget&amp;gt;<code>
</code><br />private slots:
void cellSelected(int nRow, int nCol);
private:
QTableWidget* m_pTableWidget;
QStringList m_TableHeader;<br /><code>


* mainwindow.cpp
* mainwindow.cpp


==See Also==
</code><br />#include &quot;mainwindow.h&amp;quot;
 
#include &lt;QApplication&amp;gt;<br />#include &lt;QDesktopWidget&amp;gt;<br />#include &lt;QtCore/QCoreApplication&amp;gt;<br />#include &lt;QHeaderView&amp;gt;<br />#include &lt;QMessageBox&amp;gt;
 
MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent),<br /> m_pTableWidget(NULL)<br />{<br /> m_pTableWidget = new QTableWidget(this);<br /> m_pTableWidget-&gt;setRowCount(10);<br /> m_pTableWidget-&gt;setColumnCount(3);<br /> m_TableHeader&amp;lt;&lt;&quot;#&quot;&lt;&lt;&quot;Name&amp;quot;&lt;&lt;&quot;Text&amp;quot;;<br /> m_pTableWidget-&gt;setHorizontalHeaderLabels(m_TableHeader);<br /> m_pTableWidget-&gt;verticalHeader()<s>&gt;setVisible(false);<br /> m_pTableWidget</s>&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);<br /> m_pTableWidget-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);<br /> m_pTableWidget-&gt;setSelectionMode(QAbstractItemView::SingleSelection);<br /> m_pTableWidget-&gt;setShowGrid(false);<br /> m_pTableWidget-&gt;setStyleSheet(&quot;QTableView {selection-background-color: red;}&quot;);<br /> m_pTableWidget-&gt;setGeometry(QApplication::desktop()<s>&gt;screenGeometry());
<br /> //insert data<br /> m_pTableWidget</s>&gt;setItem(0, 1, new QTableWidgetItem(&quot;Hello&amp;quot;));
 
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ),<br /> this, SLOT( cellSelected( int, int ) ) );<br />}
 
MainWindow::~MainWindow()<br />{<br />}


[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]''
void MainWindow::cellSelected(int nRow, int nCol)<br />{<br /> QMessageBox::information(this, &quot;&quot;,<br /> &quot;Cell at row &quot;''QString::number(nRow)''<br /> &quot; column &quot;''QString::number(nCol)''<br /> &quot; was double clicked.&quot;);<br />}<br /><code>


===Categories:===
== See Also ==


* [[:Category:Developing-with-Qt|Developing with Qt]]
&quot;How to use QTableWidget in Qt&amp;quot;:http://www.developer.nokia.com/Community/Wiki/index.php?title=How_to_use_QTableWidget_in_Qt
** [[:Category:Developing-with-Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 14:14, 23 February 2015




[toc align_right="yes&quot; depth="3&quot;]

English Български

How to Use QTableWidget

QTableWidget Overview

Using "QTableWidget&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtablewidget.html developers can embed tables inside Qt applications. QTableWidget inherits "QTableView&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtableview.html. Items in a QTableWidget instance are provided by class "QTableWidgetItem&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qtablewidgetitem.html.

Basic Usage

Set number of rows and columns

m_pTableWidget-&gt;setRowCount(10);<br />m_pTableWidget-&gt;setColumnCount(3);

Insert labels into the horizontal header

m_TableHeader&amp;lt;&lt;&quot;#&quot;&lt;&lt;&quot;Name&amp;quot;&lt;&lt;&quot;Text&amp;quot;;<br />m_pTableWidget-&gt;setHorizontalHeaderLabels(m_TableHeader);

Insert data

The simplest way to insert text into a cell:

m_pTableWidget-&gt;setItem(0, 1, new QTableWidgetItem(&quot;Hello&amp;quot;));<code>

==== Hide vertical header aka the line counter ====

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


h4. Hide grid


m_pTableWidget</s>&gt;setShowGrid(false);<code>

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

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

Disable editing

m_pTableWidget-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);<code>

==== Selection mode and behavior ====

The behavior of the table for selecting rows and cells can be customized using methods &quot;setSelectionBehavior&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractitemview.html#SelectionBehavior-enum and &quot;setSelectionMode&amp;quot;: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 &quot;signals for each event such as change of selection, click, double click, etc. &quot;: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 &lt;QTableWidget&amp;gt;<code>


private slots:

void cellSelected(int nRow, int nCol);

private:

QTableWidget* m_pTableWidget;

QStringList m_TableHeader;

* mainwindow.cpp


#include "mainwindow.h&quot;

  1. include <QApplication&gt;
    #include <QDesktopWidget&gt;
    #include <QtCore/QCoreApplication&gt;
    #include <QHeaderView&gt;
    #include <QMessageBox&gt;

MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent),
m_pTableWidget(NULL)
{
m_pTableWidget = new QTableWidget(this);
m_pTableWidget->setRowCount(10);
m_pTableWidget->setColumnCount(3);
m_TableHeader&lt;<"#"<<"Name&quot;<<"Text&quot;;
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&quot;));

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&quot;:http://www.developer.nokia.com/Community/Wiki/index.php?title=How_to_use_QTableWidget_in_Qt