Skip to content

Commit b1aa20b

Browse files
committed
Add QgsTableWidgetItem for easier sorting of checkbox tables
1 parent ae7b657 commit b1aa20b

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

python/gui/gui.sip

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
%Include qgsslider.sip
144144
%Include qgssublayersdialog.sip
145145
%Include qgssvgannotationitem.sip
146+
%Include qgstablewidgetitem.sip
146147
%Include qgstextannotationitem.sip
147148
%Include qgsunitselectionwidget.sip
148149
%Include qgsuserinputdockwidget.sip

python/gui/qgstablewidgetitem.sip

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***************************************************************************
2+
qgstablewidgetitem.sip - QgsTableWidgetItem
3+
4+
---------------------
5+
begin : 27.3.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
class QgsTableWidgetItem : QTableWidgetItem
18+
{
19+
%TypeHeaderCode
20+
#include "qgstablewidgetitem.h"
21+
%End
22+
23+
public:
24+
QgsTableWidgetItem();
25+
QgsTableWidgetItem( const QString& text );
26+
27+
/**
28+
* Set the role by which the items should be sorted.
29+
* By default this will be set to Qt::DisplayRole
30+
*/
31+
void setSortRole( int role );
32+
/**
33+
* Get the role by which the items should be sorted.
34+
* By default this will be Qt::DisplayRole
35+
*/
36+
int sortRole() const;
37+
};

src/gui/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ SET(QGIS_GUI_SRCS
267267
qgsslider.cpp
268268
qgssublayersdialog.cpp
269269
qgssvgannotationitem.cpp
270+
qgstablewidgetitem.cpp
270271
qgstextannotationitem.cpp
271272
qgsunitselectionwidget.cpp
272273
qgsuserinputdockwidget.cpp
@@ -575,6 +576,7 @@ SET(QGIS_GUI_HDRS
575576
qgsnumericsortlistviewitem.h
576577
qgsrubberband.h
577578
qgssvgannotationitem.h
579+
qgstablewidgetitem.h
578580
qgstextannotationitem.h
579581
qgsuserinputdockwidget.h
580582
qgsvectorlayertools.h

src/gui/qgstablewidgetitem.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgstablewidgetitem.cpp - QgsTableWidgetItem
3+
4+
---------------------
5+
begin : 27.3.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgstablewidgetitem.h"
17+
18+
QgsTableWidgetItem::QgsTableWidgetItem()
19+
: QTableWidgetItem()
20+
, mSortRole( Qt::DisplayRole )
21+
{
22+
}
23+
24+
QgsTableWidgetItem::QgsTableWidgetItem( const QString& text )
25+
: QTableWidgetItem( text )
26+
, mSortRole( Qt::DisplayRole )
27+
{
28+
}
29+
30+
void QgsTableWidgetItem::setSortRole( int role )
31+
{
32+
mSortRole = role;
33+
}
34+
35+
int QgsTableWidgetItem::sortRole() const
36+
{
37+
return mSortRole;
38+
}
39+
40+
bool QgsTableWidgetItem::operator<( const QTableWidgetItem& other ) const
41+
{
42+
#if QT_VERSION < 0x050000
43+
return data( mSortRole ).toString() < other.data( mSortRole ).toString();
44+
#else
45+
return data( mSortRole ) < other.data( mSortRole );
46+
#endif
47+
}

src/gui/qgstablewidgetitem.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/***************************************************************************
2+
qgstablewidgetitem.h - QgsTableWidgetItem
3+
4+
---------------------
5+
begin : 27.3.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSTABLEWIDGETITEM_H
17+
#define QGSTABLEWIDGETITEM_H
18+
19+
#include <QTableWidget>
20+
21+
/**
22+
* This can be used like a regular QTableWidgetItem with the difference that a
23+
* specific role can be set to sort.
24+
*/
25+
class GUI_EXPORT QgsTableWidgetItem : public QTableWidgetItem
26+
{
27+
public:
28+
QgsTableWidgetItem();
29+
/**
30+
* Creates a new table widget item with the specified text.
31+
*/
32+
QgsTableWidgetItem( const QString& text );
33+
34+
35+
/**
36+
* Set the role by which the items should be sorted.
37+
* By default this will be set to Qt::DisplayRole
38+
*/
39+
void setSortRole( int role );
40+
/**
41+
* Get the role by which the items should be sorted.
42+
* By default this will be Qt::DisplayRole
43+
*/
44+
int sortRole() const;
45+
46+
bool operator <( const QTableWidgetItem& other ) const override;
47+
48+
private:
49+
int mSortRole;
50+
};
51+
52+
#endif // QGSTABLEWIDGETITEM_H

0 commit comments

Comments
 (0)