QtBase  v6.3.1
qheaderviewtest1.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QtWidgets/QtWidgets>
30 
31 struct ManualTask {
32  const char *title;
33  const char *instructions;
34  unsigned sectionsMovable : 1;
35  unsigned selectionMode : 3;
36 };
37 
39 { QT_TR_NOOP("0. Default"),
40  "Please provide instructions",
42 },
43 { QT_TR_NOOP("1. Autoscroll"),
44  "<ol>"
45  "<li>Press and hold on section 9 of vertical header.<br/>"
46  "<em>(all cells in the row will be selected)</em>"
47  "</li>"
48  "<li>Extend the selection by moving the mouse down.<br/>"
49  "<em>(selection will extend to the next rows)</em>"
50  "</li>"
51  "<li>Continue to move the mouse down and outside the window geometry.<br/>"
52  "<em>(The view should scroll automatically and the selection should still extend)</em>"
53  "</li>"
54  "<li>While still holding the button, do the same in the opposite direction, i.e. move mouse up and outside the window geometry.<br/>"
55  "<em>(Verify that the view scrolls automatically and the selection changes)</em>"
56  "</li>"
57  "<li>Verify that it works in the other dimension, i.e Press and hold section 9 of the horizontal header.<br/>"
58  "<em>All cells in the column will be selected</em>"
59  "</li>"
60  "<li>Extend the selection by moving the mouse to the far right and outside the window geometry.<br/>"
61  "<em>(selection will extend to the next columns)</em>"
62  "</li>"
63  "<li>Verify that it works in the opposite direction (i.e. move mouse to the left of the window geometry).<br/>"
64  "<em>(Verify that the view scrolls automatically and the selection changes)</em>"
65  "</li>"
66  "</ol>",
68 }
69 
70 };
71 
72 
73 class Window : public QWidget
74 {
75  Q_OBJECT
76 public:
78  {
79  m_taskInstructions = new QLabel();
80  if (sizeof(tasks) > 0)
81  m_taskInstructions->setText(tr(tasks[0].instructions));
82 
83  QVBoxLayout *vbox = new QVBoxLayout(this);
84  vbox->addLayout(setupComboBox());
85  vbox->addWidget(setupGroupBox());
86  vbox->addWidget(setupTableView());
88  }
89 
91  {
94  cbSelectionMode->setCurrentIndex((int)sMode);
95  }
96 
97 private:
98  QFormLayout *setupComboBox()
99  {
100  QComboBox *combo = new QComboBox;
101  for (size_t i = 0; i < sizeof(tasks) / sizeof(tasks[0]); ++i) {
102  combo->addItem(tr(tasks[i].title));
103  }
104 
105  connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_taskCombo_currentIndexChanged(int)));
107  form->addRow(tr("Choose task:"), combo);
108  return form;
109  }
110 
111  QGroupBox *setupGroupBox()
112  {
113  QGroupBox *grp = new QGroupBox(tr("Properties"));
115  grp->setLayout(form);
116  ckMovable = new QCheckBox;
117  ckMovable->setObjectName(QLatin1String("ckMovable"));
118  connect(ckMovable, SIGNAL(toggled(bool)), this, SLOT(on_ckMovable_toggled(bool)));
119  form->addRow(tr("SectionsMovable"), ckMovable);
120 
122  cbSelectionMode->setObjectName(QLatin1String("cbSelectionMode"));
124  << QLatin1String("SingleSelection")
125  << QLatin1String("MultiSelection")
126  << QLatin1String("ExtendedSelection")
127  << QLatin1String("ContiguousSelection")
128  );
129 
130  connect(cbSelectionMode, SIGNAL(currentIndexChanged(int)), this, SLOT(on_cbSelectionMode_currentIndexChanged(int)));
131  form->addRow(tr("SelectionMode"), cbSelectionMode);
132  return grp;
133  }
134 
135  QTableView *setupTableView()
136  {
137  tableView = new QTableView;
138  const int rowCount = 200;
139  m.setRowCount(rowCount);
140  m.setColumnCount(250);
142  tableView->setModel(&m);
144  return tableView;
145  }
146 
147 private Q_SLOTS:
148  void on_ckMovable_toggled(bool arg)
149  {
152  }
153 
154  void on_cbSelectionMode_currentIndexChanged(int idx)
155  {
157  }
158 
159  void on_taskCombo_currentIndexChanged(int idx)
160  {
161  ManualTask &task = tasks[idx];
162  m_taskInstructions->setText(tr(task.instructions));
163  ckMovable->setChecked(task.sectionsMovable);
165  }
166 
167 public:
173 };
174 
175 class SomeHandler : public QObject
176 {
177  Q_OBJECT
178  QHeaderView *m_hv;
179  QTableView *m_tv;
180 public:
182 public slots:
183  void slotSectionResized(int, int, int);
184 };
185 
187 {
188  m_hv = hv;
189  m_tv = tv;
191  connect(hv, SIGNAL(sectionResized(int,int,int)), this, SLOT(slotSectionResized(int,int,int)));
192 }
193 void SomeHandler::slotSectionResized(int logsection, int oldsize, int newsize)
194 {
195  int offset = m_hv->offset();
196  m_tv->setUpdatesEnabled(false);
197  // Do some manual resizing - lets make every section having the new size.
198  m_hv->blockSignals(true);
199  m_hv->setDefaultSectionSize(newsize);
200  m_hv->blockSignals(false);
201 
202  // Adjust offset and scrollbar. Maybe it isn't 100% perfect
203  // but proof of concept
204  // The test has sense without the define, too.
205 #define DO_CORRECT_OFFSET_AND_SB
206 #ifdef DO_CORRECT_OFFSET_AND_SB
207  int leftRemoved = (m_hv->visualIndex(logsection)) * (oldsize - newsize);
208  int newoffset = offset - leftRemoved;
209  if (newoffset < 0)
210  newoffset = 0;
211 
212  if (newoffset > 0 && newoffset >= m_hv->count() * newsize - m_tv->viewport()->width())
213  m_hv->setOffsetToLastSection();
214  else
215  m_hv->setOffset(newoffset);
216 
217  m_tv->horizontalScrollBar()->blockSignals(true);
218  m_tv->horizontalScrollBar()->setRange(0, m_hv->count() * newsize - m_tv->viewport()->width() );
219  m_tv->horizontalScrollBar()->setValue(newoffset);
220  m_tv->horizontalScrollBar()->blockSignals(false);
221 #endif
222  m_tv->setUpdatesEnabled(true);
223 }
224 
225 int main(int argc, char *argv[])
226 {
227  QApplication app(argc, argv);
228  Window window;
229  // Comment in the line below to test selection with keyboard (space)
230  // tv.setEditTriggers(QAbstractItemView::NoEditTriggers);
231  QHeaderView *hHeader = window.tableView->horizontalHeader();
232  QHeaderView *vHeader = window.tableView->verticalHeader();
233  SomeHandler handler(hHeader, window.tableView);
234  hHeader->setDefaultSectionSize(30);
235  window.resize(600, 600);
236  window.show();
237  hHeader->setSectionsMovable(true);
238  vHeader->setSectionsMovable(true);
239  window.updateControls();
240  app.exec();
241 }
242 #include "qheaderviewtest1.moc"
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
FT_UInt idx
Definition: cffcmap.c:135
void setHorizontalScrollMode(ScrollMode mode)
SelectionMode selectionMode
which selection mode the view operates in
void setSelectionMode(QAbstractItemView::SelectionMode mode)
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
void addLayout(QLayout *layout, int stretch=0)
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:55
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:60
void addItem(const QString &text, const QVariant &userData=QVariant())
Definition: qcombobox.h:260
void addItems(const QStringList &texts)
Definition: qcombobox.h:171
void setCurrentIndex(int index)
Definition: qcombobox.cpp:2133
The QFormLayout class manages forms of input widgets and their associated labels.
Definition: qformlayout.h:54
QGraphicsObject * parent
the parent of the item
The QGroupBox widget provides a group box frame with a title.
Definition: qgroupbox.h:53
The QHeaderView class provides a header row or header column for item views.
Definition: qheaderview.h:54
void setDefaultSectionSize(int size)
void swapSections(int first, int second)
void setOffset(int offset)
void setOffsetToLastSection()
int visualIndex(int logicalIndex) const
void setSectionsMovable(bool movable)
int offset() const
bool sectionsMovable() const
int count() const
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
void setText(const QString &)
Definition: qlabel.cpp:297
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
void setObjectName(const QString &name)
Definition: qobject.cpp:1261
The QStandardItemModel class provides a generic model for storing custom data.\inmodule QtGui.
The QTableView class provides a default model/view implementation of a table view.
Definition: qtableview.h:54
void setModel(QAbstractItemModel *model) override
QHeaderView * horizontalHeader() const
QHeaderView * verticalHeader() const
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setLayout(QLayout *)
Definition: qwidget.cpp:10146
QString title
the window's title in the windowing system
Definition: qwindow.h:113
SomeHandler(QHeaderView *hv, QTableView *tv)
void slotSectionResized(int, int, int)
[Window class definition]
Definition: window.h:64
QTableView * tableView
QLabel * m_taskInstructions
void updateControls()
QStandardItemModel m
Window(QWidget *parent=nullptr)
QComboBox * cbSelectionMode
QCheckBox * ckMovable
QList< QString > QStringList
Definition: qcontainerfwd.h:64
int main(int argc, char *argv[])
[1]
ManualTask tasks[]
#define SLOT(a)
Definition: qobjectdefs.h:87
#define SIGNAL(a)
Definition: qobjectdefs.h:88
const GLfloat * m
GLenum GLuint GLintptr offset
SSL_CTX int(*) void arg)
#define tr(X)
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define Q_SLOTS
Definition: qtmetamacros.h:80
#define slots
Definition: qtmetamacros.h:76
QGraphicsWidget * form
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]
unsigned selectionMode
unsigned sectionsMovable
const char * title
const char * instructions
const int rowCount
Definition: testtable1.cpp:31