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:
parent
2614347ab8
commit
322e5b7f5e
@ -53,14 +53,15 @@
|
||||
#define CARD_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QList>
|
||||
#include <QVector>
|
||||
|
||||
class CardLayout : public QLayout
|
||||
{
|
||||
public:
|
||||
CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {}
|
||||
CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {}
|
||||
CardLayout(int dist): QLayout(dist) {}
|
||||
CardLayout(int spacing): QLayout()
|
||||
{ setSpacing(spacing); }
|
||||
CardLayout(int spacing, QWidget *parent): QLayout(parent)
|
||||
{ setSpacing(spacing); }
|
||||
~CardLayout();
|
||||
|
||||
void addItem(QLayoutItem *item) override;
|
||||
@ -72,7 +73,7 @@ public:
|
||||
void setGeometry(const QRect &rect) override;
|
||||
|
||||
private:
|
||||
QList<QLayoutItem*> list;
|
||||
QVector<QLayoutItem*> m_items;
|
||||
};
|
||||
#endif
|
||||
//! [0]
|
||||
@ -85,23 +86,23 @@ private:
|
||||
//! [2]
|
||||
int CardLayout::count() const
|
||||
{
|
||||
// QList::size() returns the number of QLayoutItems in the list
|
||||
return list.size();
|
||||
// QVector::size() returns the number of QLayoutItems in m_items
|
||||
return m_items.size();
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
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
|
||||
return list.value(idx);
|
||||
return m_items.value(idx);
|
||||
}
|
||||
|
||||
QLayoutItem *CardLayout::takeAt(int idx)
|
||||
{
|
||||
// QList::take does not do index checking
|
||||
return idx >= 0 && idx < list.size() ? list.takeAt(idx) : 0;
|
||||
// QVector::take does not do index checking
|
||||
return idx >= 0 && idx < m_items.size() ? m_items.takeAt(idx) : 0;
|
||||
}
|
||||
//! [3]
|
||||
|
||||
@ -109,7 +110,7 @@ QLayoutItem *CardLayout::takeAt(int idx)
|
||||
//! [4]
|
||||
void CardLayout::addItem(QLayoutItem *item)
|
||||
{
|
||||
list.append(item);
|
||||
m_items.append(item);
|
||||
}
|
||||
//! [4]
|
||||
|
||||
@ -129,14 +130,14 @@ void CardLayout::setGeometry(const QRect &r)
|
||||
{
|
||||
QLayout::setGeometry(r);
|
||||
|
||||
if (list.size() == 0)
|
||||
if (m_items.size() == 0)
|
||||
return;
|
||||
|
||||
int w = r.width() - (list.count() - 1) * spacing();
|
||||
int h = r.height() - (list.count() - 1) * spacing();
|
||||
int w = r.width() - (m_items.count() - 1) * spacing();
|
||||
int h = r.height() - (m_items.count() - 1) * spacing();
|
||||
int i = 0;
|
||||
while (i < list.size()) {
|
||||
QLayoutItem *o = list.at(i);
|
||||
while (i < m_items.size()) {
|
||||
QLayoutItem *o = m_items.at(i);
|
||||
QRect geom(r.x() + i * spacing(), r.y() + i * spacing(), w, h);
|
||||
o->setGeometry(geom);
|
||||
++i;
|
||||
@ -148,29 +149,29 @@ void CardLayout::setGeometry(const QRect &r)
|
||||
//! [7]
|
||||
QSize CardLayout::sizeHint() const
|
||||
{
|
||||
QSize s(0,0);
|
||||
int n = list.count();
|
||||
QSize s(0, 0);
|
||||
int n = m_items.count();
|
||||
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;
|
||||
while (i < n) {
|
||||
QLayoutItem *o = list.at(i);
|
||||
QLayoutItem *o = m_items.at(i);
|
||||
s = s.expandedTo(o->sizeHint());
|
||||
++i;
|
||||
}
|
||||
return s + n*QSize(spacing(), spacing());
|
||||
return s + n * QSize(spacing(), spacing());
|
||||
}
|
||||
|
||||
QSize CardLayout::minimumSize() const
|
||||
{
|
||||
QSize s(0,0);
|
||||
int n = list.count();
|
||||
QSize s(0, 0);
|
||||
int n = m_items.count();
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
QLayoutItem *o = list.at(i);
|
||||
QLayoutItem *o = m_items.at(i);
|
||||
s = s.expandedTo(o->minimumSize());
|
||||
++i;
|
||||
}
|
||||
return s + n*QSize(spacing(), spacing());
|
||||
return s + n * QSize(spacing(), spacing());
|
||||
}
|
||||
//! [7]
|
||||
|
@ -89,11 +89,10 @@ dialog.setNameFilter("*.cpp *.cc *.C *.cxx *.c++");
|
||||
|
||||
|
||||
//! [7]
|
||||
QStringList filters;
|
||||
filters << "Image files (*.png *.xpm *.jpg)"
|
||||
<< "Text files (*.txt)"
|
||||
<< "Any files (*)";
|
||||
|
||||
const QStringList filters({"Image files (*.png *.xpm *.jpg)",
|
||||
"Text files (*.txt)",
|
||||
"Any files (*)"
|
||||
});
|
||||
QFileDialog dialog(this);
|
||||
dialog.setNameFilters(filters);
|
||||
dialog.exec();
|
||||
@ -131,10 +130,10 @@ QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
|
||||
//! [12]
|
||||
|
||||
//! [13]
|
||||
QStringList mimeTypeFilters;
|
||||
mimeTypeFilters << "image/jpeg" // will show "JPEG image (*.jpeg *.jpg *.jpe)
|
||||
<< "image/png" // will show "PNG image (*.png)"
|
||||
<< "application/octet-stream"; // will show "All files (*)"
|
||||
QStringList mimeTypeFilters({"image/jpeg", // will show "JPEG image (*.jpeg *.jpg *.jpe)
|
||||
"image/png", // will show "PNG image (*.png)"
|
||||
"application/octet-stream" // will show "All files (*)"
|
||||
});
|
||||
|
||||
QFileDialog dialog(this);
|
||||
dialog.setMimeTypeFilters(mimeTypeFilters);
|
||||
|
@ -53,6 +53,6 @@ QTreeWidget *treeWidget = new QTreeWidget();
|
||||
treeWidget->setColumnCount(1);
|
||||
QList<QTreeWidgetItem *> items;
|
||||
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);
|
||||
//! [0]
|
||||
|
@ -51,9 +51,10 @@
|
||||
//! [0]
|
||||
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"))
|
||||
return new QCoreApplication(argc, argv);
|
||||
}
|
||||
return new QApplication(argc, argv);
|
||||
}
|
||||
|
||||
@ -187,14 +188,14 @@ for (const QString &command : commands)
|
||||
|
||||
|
||||
//! [12]
|
||||
QWidget *widget = qApp->widgetAt(x, y);
|
||||
QWidget *widget = QApplication::widgetAt(x, y);
|
||||
if (widget)
|
||||
widget = widget->window();
|
||||
//! [12]
|
||||
|
||||
|
||||
//! [13]
|
||||
QWidget *widget = qApp->widgetAt(point);
|
||||
QWidget *widget = QApplication::widgetAt(point);
|
||||
if (widget)
|
||||
widget = widget->window();
|
||||
//! [13]
|
||||
|
@ -70,8 +70,9 @@ void MyWidget::paintEvent(QPaintEvent *)
|
||||
|
||||
//! [1]
|
||||
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]
|
||||
|
@ -52,8 +52,7 @@
|
||||
int MyLayout::heightForWidth(int w) const
|
||||
{
|
||||
if (cache_dirty || cached_width != w) {
|
||||
// not all C++ compilers support "mutable"
|
||||
MyLayout *that = (MyLayout*)this;
|
||||
MyLayout *that = const_cast<MyLayout *>(this);
|
||||
int h = calculateHeightForWidth(w);
|
||||
that->cached_hfw = h;
|
||||
return h;
|
||||
|
@ -53,8 +53,8 @@ class MyProxyStyle : public QProxyStyle
|
||||
{
|
||||
public:
|
||||
|
||||
int styleHint(StyleHint hint, const QStyleOption *option = 0,
|
||||
const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override
|
||||
int styleHint(StyleHint hint, const QStyleOption *option = nullptr,
|
||||
const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
|
||||
{
|
||||
if (hint == QStyle::SH_UnderlineShortcut)
|
||||
return 1;
|
||||
@ -72,8 +72,8 @@ public:
|
||||
class MyProxyStyle : public QProxyStyle
|
||||
{
|
||||
public:
|
||||
int styleHint(StyleHint hint, const QStyleOption *option = 0,
|
||||
const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override
|
||||
int styleHint(StyleHint hint, const QStyleOption *option = nullptr,
|
||||
const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
|
||||
{
|
||||
if (hint == QStyle::SH_UnderlineShortcut)
|
||||
return 0;
|
||||
|
@ -54,5 +54,5 @@ menubar->addMenu(fileMenu);
|
||||
|
||||
|
||||
//! [1]
|
||||
QMenuBar *menuBar = new QMenuBar(0);
|
||||
QMenuBar *menuBar = new QMenuBar(nullptr);
|
||||
//! [1]
|
||||
|
@ -56,10 +56,10 @@ splash->show();
|
||||
... // Loading some items
|
||||
splash->showMessage("Loaded modules");
|
||||
|
||||
qApp->processEvents();
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
... // Establishing connections
|
||||
splash->showMessage("Established connections");
|
||||
|
||||
qApp->processEvents();
|
||||
QCoreApplication::processEvents();
|
||||
//! [0]
|
||||
|
@ -300,7 +300,7 @@
|
||||
\list
|
||||
\li A data structure to store the items handled by the layout. Each
|
||||
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::}{setGeometry()}, how to perform the layout.
|
||||
\li \l{QLayout::}{sizeHint()}, the preferred size of the layout.
|
||||
|
Loading…
x
Reference in New Issue
Block a user