关于QWidget *parent=0的理解

QWidget:

Widget 是使用Qt编写的图形用户界面(GUI)应用程序的基本生成块。每个GUI组件,如按钮、标签或文本编辑器,都是一个Widget,并可
以放置在现有的用户界面中或作为单独的窗口显示。每种类型的组件都是由QWidget 的特殊子类提供的,而QWidget又是QObject的子类。

QWidget是所有Qt GUI界面类的基类,它接收鼠标、键盘及其他窗口事件,并在显示器上绘制自己。

通过传入QWidget 构造函数的参数(或者调用QWidget:setWindowFlags()QWidget::setParent()函数)可以指定一个窗口部件的窗口标识( window flags)父窗口部件

窗口标识( window flags):

窗口标识定义了,该部件的窗口类型窗口提示(hint)

窗口类型指定了窗口部件的窗口系统属性( window-system properties); 仅一个。

窗口提示定义了顶层窗口的外观,-一个窗口可以有多个提示(提示能够进行按位或操作)。
 

窗口

窗口通常具有一个窗口边框( frame)个标题栏

根据有无边框和标题栏,可分为,子窗口部件窗口

QMainWindow和所有的QDialog对话框子类都是经常使用的窗口类型,而子窗口部件通
常处在父窗口部件的内部,没有窗口边框和标题栏,没有父窗口部件的Widget对象就是一个窗口。

 QWidget窗口部件的构造函数为: QWidget (QWidget *parent=0, Qt: :WindowFlags f=0)


其中,参数parent指定了窗口部件的父窗口部件;

1、如果parent=0 ( 默认值),则新建的窗口部件一定将是一个窗口

2、如果parent != 0,则新建的窗口部件可能是子窗口部件也有可能是一个窗口,由它参数Qt::WindowFlags属性决定;

假设是窗口部件的话,它就只会出现在父窗口部件的界面内部。参数f指定了新窗口部件的窗口标识,默认值是0,即Qt::Widget。

Qt::WindowFlags属性

Qt::Widget               //是一个窗口或部件,有父窗口就是部件,没有就是窗口 默认为0
Qt::Window               //是一个窗口,有窗口边框和标题
Qt::Dialog               //是一个对话框窗口
Qt::Sheet                //是一个窗口或部件Macintosh表单
Qt::Drawer               //是一个窗口或部件Macintosh抽屉,去掉窗口左上角的图标
Qt::Popup                //是一个弹出式顶层窗口
Qt::Tool                 //是一个工具窗口
Qt::ToolTip              //是一个提示窗口,没有标题栏和窗口边框
Qt::SplashScreen         //是一个欢迎窗口,是QSplashScreen构造函数的默认值
Qt::Desktop              //是一个桌面窗口或部件
Qt::SubWindow            //是一个子窗口


额外属性

Qt::CustomizeWindowHint          //关闭默认窗口标题提示
Qt::WindowTitleHint              //为窗口修饰一个标题栏
Qt::WindowSystemMenuHint         //为窗口修饰一个窗口菜单系统
Qt::WindowMinimizeButtonHint     //为窗口添加最小化按钮
Qt::WindowMaximizeButtonHint     //为窗口添加最大化按钮
Qt::WindowMinMaxButtonsHint      //为窗口添加最大化和最小化按钮
Qt::WindowCloseButtonHint            //窗口只有一个关闭按钮
Qt::WindowContextHelpButtonHint
Qt::MacWindowToolBarButtonHint
Qt::WindowFullscreenButtonHint
Qt::BypassGraphicsProxyWidget
Qt::WindowShadeButtonHint
Qt::WindowStaysOnTopHint    //总在最上面的窗口,置前
Qt::WindowStaysOnBottomHint
Qt::WindowOkButtonHint
Qt::WindowCancelButtonHint
Qt::WindowTransparentForInput


this->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);//去掉最大化按钮    就用符号~


 

#ifndef ITEMVIEW_H #define ITEMVIEW_H class ItemView : public QWidget { Q_OBJECT public: explicit ItemView(const QString &key, QWidget *parent = nullptr); explicit ItemView(const QString &key, const QString &rightText, QWidget *parent = nullptr); explicit ItemView(const QString &key, const QString &leftText, const QString &rightText, QWidget *parent = nullptr); QVariant getValue() const; void setValue(const QVariant &newValue); void setSpacing(int spacing); void setMargins(int left, int up, int right, int button); void setValidator(const QValidator *v); signals: void valueChanged(); private: QHBoxLayout *layout; QLabel *keyLabel; QLabel *leftLabel; QLabel *rightLabel; QLineEdit *lineEdit; QVariant value; }; #endif // ITEMVIEW_H #include “itemview.h” ItemView::ItemView(const QString &key, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(lineEdit); this->setLayout(layout); } ItemView::ItemView(const QString &key, const QString &rightText, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); rightLabel = new QLabel(rightText, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(rightLabel); layout->addWidget(lineEdit); this->setLayout(layout); } ItemView::ItemView(const QString &key, const QString &leftText, const QString &rightText, QWidget *parent) : QWidget{parent} { keyLabel = new QLabel(key, this); leftLabel = new QLabel(leftText, this); rightLabel = new QLabel(rightText, this); lineEdit = new QLineEdit(this); layout = new QHBoxLayout(this); layout->addWidget(keyLabel); layout->addWidget(leftLabel); layout->addWidget(rightLabel); layout->addWidget(lineEdit); this->setLayout(layout); } QVariant ItemView::getValue() const { value = QVariant(lineEdit->text()); return value; } void ItemView::setValue(const QVariant &newValue) { if (value == newValue) return; value = newValue; lineEdit->setText(value.toString()); emit valueChanged(); } void ItemView::setSpacing(int spacing) { layout->setSpacing(spacing); } void ItemView::setMargins(int left, int up, int right, int button) { layout->setContentsMargins(left, up, right, button); } void ItemView::setValidator(const QValidator *v) { lineEdit->setValidator(v); } 上述代码报错,itemview.cpp:89:11: No viable overloaded ‘=’ qvariant.h:332:15: candidate function not viable: ‘this’ argument has type ‘const QVariant’, but method is not marked const :335:5: note: candidate function not viable: ‘this’ argument has type ‘const QVariant’, but method is not marked const如何修改
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闫有尽意无琼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值