jingweimo的个人博客分享 http://blog.sciencenet.cn/u/jingweimo

博文

Create Qt widgets

已有 2413 次阅读 2018-2-10 05:37 |系统分类:科研笔记

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:

  1. 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).

  2. 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/10591059/meaning-of-qwidget-parent-0-in-constructor-myclassqwidget-parent-0

https://stackoverflow.com/questions/8798313/what-is-the-purpose-of-qwidgets-parent

http://www.cplusplus.com/doc/tutorial/  

https://stackoverflow.com/questions/4984600/when-do-i-use-a-dot-arrow-or-double-colon-to-refer-to-members-of-a-class-in-c

https://stackoverflow.com/questions/8125976/in-c-what-does-mean

 



https://blog.sciencenet.cn/blog-578676-1099222.html

上一篇:Summary of the issues with Qt-OpenCV
下一篇:Conversion between QImage and CV Images
收藏 IP: 35.10.57.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-27 07:37

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部