QtWidget docs: small fixes

Apply some minor fixes to the widget docs
 - use nullptr
 - use c++11 initializer list
 - properly delete widget when cleaning the layout (QTBUG-29471)
 - rework CardLayout example to make it work with Qt5

Fixes: QTBUG-29471
Change-Id: Ie2ee9f75eb8faf97d2bbd2c25e7013d4f30d8dd0
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-12-18 16:05:52 +01:00
parent 2614347ab8
commit 322e5b7f5e
10 changed files with 52 additions and 51 deletions

View File

@ -53,14 +53,15 @@
#define CARD_H #define CARD_H
#include <QtWidgets> #include <QtWidgets>
#include <QList> #include <QVector>
class CardLayout : public QLayout class CardLayout : public QLayout
{ {
public: public:
CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {} CardLayout(int spacing): QLayout()
CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {} { setSpacing(spacing); }
CardLayout(int dist): QLayout(dist) {} CardLayout(int spacing, QWidget *parent): QLayout(parent)
{ setSpacing(spacing); }
~CardLayout(); ~CardLayout();
void addItem(QLayoutItem *item) override; void addItem(QLayoutItem *item) override;
@ -72,7 +73,7 @@ public:
void setGeometry(const QRect &rect) override; void setGeometry(const QRect &rect) override;
private: private:
QList<QLayoutItem*> list; QVector<QLayoutItem*> m_items;
}; };
#endif #endif
//! [0] //! [0]
@ -85,23 +86,23 @@ private:
//! [2] //! [2]
int CardLayout::count() const int CardLayout::count() const
{ {
// QList::size() returns the number of QLayoutItems in the list // QVector::size() returns the number of QLayoutItems in m_items
return list.size(); return m_items.size();
} }
//! [2] //! [2]
//! [3] //! [3]
QLayoutItem *CardLayout::itemAt(int idx) const QLayoutItem *CardLayout::itemAt(int idx) const
{ {
// QList::value() performs index checking, and returns 0 if we are // QVector::value() performs index checking, and returns nullptr if we are
// outside the valid range // outside the valid range
return list.value(idx); return m_items.value(idx);
} }
QLayoutItem *CardLayout::takeAt(int idx) QLayoutItem *CardLayout::takeAt(int idx)
{ {
// QList::take does not do index checking // QVector::take does not do index checking
return idx >= 0 && idx < list.size() ? list.takeAt(idx) : 0; return idx >= 0 && idx < m_items.size() ? m_items.takeAt(idx) : 0;
} }
//! [3] //! [3]
@ -109,7 +110,7 @@ QLayoutItem *CardLayout::takeAt(int idx)
//! [4] //! [4]
void CardLayout::addItem(QLayoutItem *item) void CardLayout::addItem(QLayoutItem *item)
{ {
list.append(item); m_items.append(item);
} }
//! [4] //! [4]
@ -129,14 +130,14 @@ void CardLayout::setGeometry(const QRect &r)
{ {
QLayout::setGeometry(r); QLayout::setGeometry(r);
if (list.size() == 0) if (m_items.size() == 0)
return; return;
int w = r.width() - (list.count() - 1) * spacing(); int w = r.width() - (m_items.count() - 1) * spacing();
int h = r.height() - (list.count() - 1) * spacing(); int h = r.height() - (m_items.count() - 1) * spacing();
int i = 0; int i = 0;
while (i < list.size()) { while (i < m_items.size()) {
QLayoutItem *o = list.at(i); QLayoutItem *o = m_items.at(i);
QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h); QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
o->setGeometry(geom); o->setGeometry(geom);
++i; ++i;
@ -148,29 +149,29 @@ void CardLayout::setGeometry(const QRect &r)
//! [7] //! [7]
QSize CardLayout::sizeHint() const QSize CardLayout::sizeHint() const
{ {
QSize s(0,0); QSize s(0, 0);
int n = list.count(); int n = m_items.count();
if (n > 0) if (n > 0)
s = QSize(100,70); //start with a nice default size s = QSize(100, 70); //start with a nice default size
int i = 0; int i = 0;
while (i < n) { while (i < n) {
QLayoutItem *o = list.at(i); QLayoutItem *o = m_items.at(i);
s = s.expandedTo(o->sizeHint()); s = s.expandedTo(o->sizeHint());
++i; ++i;
} }
return s + n*QSize(spacing(), spacing()); return s + n * QSize(spacing(), spacing());
} }
QSize CardLayout::minimumSize() const QSize CardLayout::minimumSize() const
{ {
QSize s(0,0); QSize s(0, 0);
int n = list.count(); int n = m_items.count();
int i = 0; int i = 0;
while (i < n) { while (i < n) {
QLayoutItem *o = list.at(i); QLayoutItem *o = m_items.at(i);
s = s.expandedTo(o->minimumSize()); s = s.expandedTo(o->minimumSize());
++i; ++i;
} }
return s + n*QSize(spacing(), spacing()); return s + n * QSize(spacing(), spacing());
} }
//! [7] //! [7]

View File

@ -89,11 +89,10 @@ dialog.setNameFilter("*.cpp *.cc *.C *.cxx *.c++");
//! [7] //! [7]
QStringList filters; const QStringList filters({"Image files (*.png *.xpm *.jpg)",
filters << "Image files (*.png *.xpm *.jpg)" "Text files (*.txt)",
<< "Text files (*.txt)" "Any files (*)"
<< "Any files (*)"; });
QFileDialog dialog(this); QFileDialog dialog(this);
dialog.setNameFilters(filters); dialog.setNameFilters(filters);
dialog.exec(); dialog.exec();
@ -131,10 +130,10 @@ QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
//! [12] //! [12]
//! [13] //! [13]
QStringList mimeTypeFilters; QStringList mimeTypeFilters({"image/jpeg", // will show "JPEG image (*.jpeg *.jpg *.jpe)
mimeTypeFilters << "image/jpeg" // will show "JPEG image (*.jpeg *.jpg *.jpe) "image/png", // will show "PNG image (*.png)"
<< "image/png" // will show "PNG image (*.png)" "application/octet-stream" // will show "All files (*)"
<< "application/octet-stream"; // will show "All files (*)" });
QFileDialog dialog(this); QFileDialog dialog(this);
dialog.setMimeTypeFilters(mimeTypeFilters); dialog.setMimeTypeFilters(mimeTypeFilters);

View File

@ -53,6 +53,6 @@ QTreeWidget *treeWidget = new QTreeWidget();
treeWidget->setColumnCount(1); treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items; QList<QTreeWidgetItem *> items;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i)))); items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(0, items); treeWidget->insertTopLevelItems(0, items);
//! [0] //! [0]

View File

@ -51,9 +51,10 @@
//! [0] //! [0]
QCoreApplication* createApplication(int &argc, char *argv[]) QCoreApplication* createApplication(int &argc, char *argv[])
{ {
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i) {
if (!qstrcmp(argv[i], "-no-gui")) if (!qstrcmp(argv[i], "-no-gui"))
return new QCoreApplication(argc, argv); return new QCoreApplication(argc, argv);
}
return new QApplication(argc, argv); return new QApplication(argc, argv);
} }
@ -187,14 +188,14 @@ for (const QString &command : commands)
//! [12] //! [12]
QWidget *widget = qApp->widgetAt(x, y); QWidget *widget = QApplication::widgetAt(x, y);
if (widget) if (widget)
widget = widget->window(); widget = widget->window();
//! [12] //! [12]
//! [13] //! [13]
QWidget *widget = qApp->widgetAt(point); QWidget *widget = QApplication::widgetAt(point);
if (widget) if (widget)
widget = widget->window(); widget = widget->window();
//! [13] //! [13]

View File

@ -70,8 +70,9 @@ void MyWidget::paintEvent(QPaintEvent *)
//! [1] //! [1]
QLayoutItem *child; QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) { while ((child = layout->takeAt(0)) != nullptr) {
... ...
delete child; delete child->widget(); // delete the widget
delete child; // delete the layout item
} }
//! [1] //! [1]

View File

@ -52,8 +52,7 @@
int MyLayout::heightForWidth(int w) const int MyLayout::heightForWidth(int w) const
{ {
if (cache_dirty || cached_width != w) { if (cache_dirty || cached_width != w) {
// not all C++ compilers support "mutable" MyLayout *that = const_cast<MyLayout *>(this);
MyLayout *that = (MyLayout*)this;
int h = calculateHeightForWidth(w); int h = calculateHeightForWidth(w);
that->cached_hfw = h; that->cached_hfw = h;
return h; return h;

View File

@ -53,8 +53,8 @@ class MyProxyStyle : public QProxyStyle
{ {
public: public:
int styleHint(StyleHint hint, const QStyleOption *option = 0, int styleHint(StyleHint hint, const QStyleOption *option = nullptr,
const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
{ {
if (hint == QStyle::SH_UnderlineShortcut) if (hint == QStyle::SH_UnderlineShortcut)
return 1; return 1;
@ -72,8 +72,8 @@ public:
class MyProxyStyle : public QProxyStyle class MyProxyStyle : public QProxyStyle
{ {
public: public:
int styleHint(StyleHint hint, const QStyleOption *option = 0, int styleHint(StyleHint hint, const QStyleOption *option = nullptr,
const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
{ {
if (hint == QStyle::SH_UnderlineShortcut) if (hint == QStyle::SH_UnderlineShortcut)
return 0; return 0;

View File

@ -54,5 +54,5 @@ menubar->addMenu(fileMenu);
//! [1] //! [1]
QMenuBar *menuBar = new QMenuBar(0); QMenuBar *menuBar = new QMenuBar(nullptr);
//! [1] //! [1]

View File

@ -56,10 +56,10 @@ splash->show();
... // Loading some items ... // Loading some items
splash->showMessage("Loaded modules"); splash->showMessage("Loaded modules");
qApp->processEvents(); QCoreApplication::processEvents();
... // Establishing connections ... // Establishing connections
splash->showMessage("Established connections"); splash->showMessage("Established connections");
qApp->processEvents(); QCoreApplication::processEvents();
//! [0] //! [0]

View File

@ -300,7 +300,7 @@
\list \list
\li A data structure to store the items handled by the layout. Each \li A data structure to store the items handled by the layout. Each
item is a \l{QLayoutItem}{QLayoutItem}. We will use a item is a \l{QLayoutItem}{QLayoutItem}. We will use a
QList in this example. QVector in this example.
\li \l{QLayout::}{addItem()}, how to add items to the layout. \li \l{QLayout::}{addItem()}, how to add items to the layout.
\li \l{QLayout::}{setGeometry()}, how to perform the layout. \li \l{QLayout::}{setGeometry()}, how to perform the layout.
\li \l{QLayout::}{sizeHint()}, the preferred size of the layout. \li \l{QLayout::}{sizeHint()}, the preferred size of the layout.