|
QWidget (http://doc.qt.io/qt-5/qwidget.html)
The QWidget class is the base class of all user interface objects.
The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order. A widget is clipped by its parent and by the widgets in front of it.
A widget that is not embedded in a parent widget is called a window. Usually, windows have a frame and a title bar, although it is also possible to create windows without such decoration using suitable window flags). In Qt, QMainWindow and the various subclasses of QDialog are the most common window types.
Every widget's constructor accepts one or two standard arguments:
QWidget *parent = 0
is the parent of the new widget. If it is 0 (the default), the new widget will be a window. If not, it will be a child of parent, and be constrained by parent's geometry (unless you specify Qt::Window as window flag).
Qt::WindowFlags f = 0
(where available) sets the window flags; the default is suitable for almost all widgets, but to get, for example, a window without a window system frame, you must use special flags.
QWidget has many member functions, but some of them have little direct functionality; for example, QWidget has a font property, but never uses this itself. There are many subclasses which provide real functionality, such as QLabel, QPushButton, QListWidget, and QTabWidget.
Example: create a HexSpinBox widget
#ifndefHEXSPINBOX_H
#defineHEXSPINBOX_H
#include<QSpinBox>
QT_BEGIN_NAMESPACE
classQRegExpValidator;//acceptbetweenoneandeightcharacters
QT_END_NAMESPACE
classHexSpinBox:publicQSpinBox//inheritedfromQSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget*parent=0);//defineaconstructor
protected:
QValidator::Statevalidate(QString&text,int&pos)const;
intvalueFromText(constQString&text)const;
QStringtextFromValue(intvalue)const;
private:
QRegExpValidator*validator;
};
#endif
#include<QtWidgets>
#include"hexspinbox.h"
HexSpinBox::HexSpinBox(QWidget*parent)
:QSpinBox(parent)
{
setRange(0,255);
validator=newQRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"),this);
}
QValidator::StateHexSpinBox::validate(QString&text,int&pos)const
{
returnvalidator->validate(text,pos);
}
intHexSpinBox::valueFromText(constQString&text)const
{
boolok;
returntext.toInt(&ok,16);
}
QStringHexSpinBox::textFromValue(intvalue)const
{
returnQString::number(value,16).toUpper();
}
Sources:
https://stackoverflow.com/questions/8798313/what-is-the-purpose-of-qwidgets-parent
http://www.cplusplus.com/doc/tutorial/
https://stackoverflow.com/questions/8125976/in-c-what-does-mean
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 07:37
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社