文章目录
- 1.测试工程配置
- 2.成员函数
-
- 2.1 void setAcceptDrops(bool on)
- 2.2 bool acceptDrops() const
- 2.3 void setAccessibleDescription(const QString &description)
- 2.4 bool acceptDescription() const
- 2.5 void setAccessibleName(const QString &name)
- 2.6 QString accessibleName() const
- 2.7 void setAttribute(Qt::WidgetAttribute attribute, bool on = true)
- 2.8 bool QWidget::testAttribute(Qt::WidgetAttribute attribute) const
- 2.9 void setAutoFillBackground(bool enabled)
- 2.10 bool autoFillBackground() const
- 2.11 void setBackgroundRole(QPalette::ColorRole role)
- 2.12 QPalette::ColorRole QWidget::backgroundRole() const
- 2.13 void setBaseSize(const QSize &)
- 2.14 QSize baseSize() const
- 2.15 void setBaseSize(int basew, int baseh)
- 2.16 void setSizeIncrement(const QSize &)
- 2.17 void setSizeIncrement(int w, int h)
- 2.18 QSize sizeIncrement() const
- 2.19 void setContentsMargins(int left, int top, int right, int bottom)
- 2.20 void setContentsMargins(const QMargins &margins)
- 2.21 QRect QWidget::contentsRect() const
- 2.22 QMargins QWidget::contentsMargins() const
- 2.23 void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
- 2.24 void setCursor(const QCursor &)
- 2.25 void unsetCursor()
- 2.26 QCursor cursor() const
1.测试工程配置
创建名为QtGuiAppTest的qwidget工程,带ui文件
2.成员函数
继承自QObject和QPaintDevice。QObject详见QObject各成员函数功能详细说明(文字+用例+代码+效果图)
2.1 void setAcceptDrops(bool on)
acceptDrops : bool这个属性代表放下事件(drop events)在当前widget是否是使能的;将该属性设置为true,即是向系统声明了当前widget可以接受放下事件。
如果当前widget是桌面,则当其他应用正在使用桌面时,设置true的行为可能会失败。可以通过调用acceptDrops()判断widget是否接收放下事件。具体使用参考另一篇文章《图形视图框架》中的拖拽机制。
2.2 bool acceptDrops() const
获取widget是否能接收放下事件的状态,true为能接收。
2.3 void setAccessibleDescription(const QString &description)
accessibleDescription : QString这个属性保存了widget的描述,该描述可以通过辅助技术手段看到(例如辅助显示的客户端,提供视觉外观的文本说明,可以为视弱或盲人用户提供更多的上下文,也可用于上下文搜索或其他应用程序)。
一个widget的描述应该传递这个widget是做什么的的描述。当一个widget的accessibleName是一个简短的字符串(例如:保存),那么这个描述应该给更多的上下文信息(例如:保存当前文档)。默认情况下,该属性包含一个空的字符串。
2.4 bool acceptDescription() const
获取属性accessibleDescription的内容
2.5 void setAccessibleName(const QString &name)
accessibleName : QString 这个属性持有widget的名字,该名字可以通过辅助技术手段看到(说明见第2.3节)。当通过辅助技术手段例如通过屏幕阅读器去阅览界面的时候,该名字表示了一个widget。对于多数widget的设置,这个属性并不需要。例如对于qpushbutton的对象,使用按钮的text即可。
还有一种情况该属性显得比较重要,就是当widget不提供任何的文本。例如一个仅包含图片的按钮,需要设置这个属性,在屏幕阅读器中起到关键作用(让使用者知道存在一个按钮)。这个名称应该是简短且能传递出widget的视觉信息的。默认情况下,该字段包含一个空字符串。
2.6 QString accessibleName() const
获取属性accessibleName的内容
2.7 void setAttribute(Qt::WidgetAttribute attribute, bool on = true)
如果参数on为true,则调用该函数可设置名称为attribute的控件属性。否则,如果参数on为false,则调用该函数作用为清除属性attribute
Qt::WidgetAttribute为枚举值。
2.8 bool QWidget::testAttribute(Qt::WidgetAttribute attribute) const
如果指定的属性attribute已经设置在这个控件上,则返回true,否则返回false
2.9 void setAutoFillBackground(bool enabled)
autoFillBackground : bool 这个属性决定当前控件背景是否是自动填充
如果设置为true,这个属性将导致在调用绘制事件(paint event)之前,qt去填充控件的背景。填充的颜色由控件的调色板的QPalette::Window定义
窗口总是使用QPalette::Window来填充,除非WA_OpaquePaintEvent或WA_NoSystemBackground属性被设置了
如果控件的父类对背景色有一个静态渐变设置,那么这个属性就不能关闭。
默认该属性为false。应用例子见2.11节。
2.10 bool autoFillBackground() const
获取是否自动背景填充的状态
2.11 void setBackgroundRole(QPalette::ColorRole role)
(1)功能说明
将控件的背景角色设置为参数role。
背景角色定义了来自控件调色板( QPalette)的刷子(即刷子的颜色由控件调色板的枚举值确定),被用来渲染背景。
如果参数role的值是QPalette::NoRole,则控件继承父类背景角色。
QPalette::ColorRole详细定义如下:
常量名 | 常量值 | 描述 |
---|---|---|
QPalette::Window | 10 | 一个通用的背景颜色 |
QPalette::Background | 这个值无效,请使用 QPalette::Window替代 | |
QPalette::WindowText | 0 | 一个通用的前景颜色 |
QPalette::Foreground | 这个值无效,请使用QPalette::WindowText替代 | |
QPalette::Base | 9 | 用于大多数文本输入控件的背景颜色,但也用于其他的绘制-例如combobox下拉列表和工具栏的背景颜色,它通常是白色或其他浅色 |
QPalette::AlternateBase | 16 | 用作具有交替行颜色的视图中的备用背景色 |
QPalette::ToolTipBase | 18 | 用作qtooltip和qwhatthis的背景颜色。qtooltip使用调色板中的非活动颜色组,因为tooltips是不活动窗口 |
QPalette::ToolTipText | 19 | 用作qtooltip和qwhatthis的背景颜色。qtooltip使用调色板中的非活动颜色组,因为tooltips是不活动窗口 |
QPalette::PlaceholderText | 20 | 用作各种文本输入控件的占位符颜色 |
QPalette::Text | 6 | 背景颜色用Base。通常与 WindowText相同 |
QPalette::Button | 1 | 通用按钮背景颜色,不能与Window相同 |
QPalette::ButtonText | 8 | 一个前景颜色,使用QPalette::Button |
QPalette::BrightText | 7 | 文本颜色,与QPalette::WindowText有较大差别,例如黑色。 |
(2)效果展示
点击按钮,设置背景颜色为黑色
(3)调用程序
QtGuiAppTest.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
class QtGuiAppTest : public QWidget
{
Q_OBJECT
public:
QtGuiAppTest(QWidget *parent = Q_NULLPTR);
~QtGuiAppTest();
public slots:
void slottest1();
private:
Ui::QtGuiAppTestClass ui;
};
QtGuiAppTest.cpp
#include "QtGuiAppTest.h"
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.pushButton, &QPushButton::clicked, this, &QtGuiAppTest::slottest1);
}
QtGuiAppTest::~QtGuiAppTest()
{
}
void QtGuiAppTest::slottest1()
{
//设置背景填充色
setBackgroundRole(QPalette::Dark);
//打开自动填充
setAutoFillBackground(true);
}
main.cpp
#include "QtGuiAppTest.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtGuiAppTest w;
w.show();
return a.exec();
}
2.12 QPalette::ColorRole QWidget::backgroundRole() const
获取当前设置的背景色。
2.13 void setBaseSize(const QSize &)
baseSize : QSize,该属性控制控件的基本尺寸。
如果控件定义了sizeIncrement(),基本尺寸用于计算一个合适的控件的大小。
默认情况,对于一个新建控件,这个属性包含为0的宽与高的尺寸。
2.14 QSize baseSize() const
获取控件的基础尺寸。
2.15 void setBaseSize(int basew, int baseh)
设置当前控件的基础尺寸宽为basew,高为baseh。
2.16 void setSizeIncrement(const QSize &)
sizeIncrement : QSize 该属性存储了控件的尺寸增量
当用户调整窗口大小,尺寸大小将按照尺寸增量属性设置的大小作为步长改变。宽度改变步长为sizeIncrement().width(),高度改变步长为sizeIncrement.height(),baseSize()作为基础尺寸。设置控件尺寸为非负整数i,j:
调整后的宽度为width = baseSize().width() + i * sizeIncrement().width();
调整后的高度为height = baseSize().height() + j * sizeIncrement().height();
当为所有的控件设置尺寸增量时,该属性仅影响窗口控件。
默认情况下,该属性包含一个高度和宽度都为0的尺寸。
在windows系统下尺寸增量不起作用,可能在X11的窗口管理器中被忽略掉了。
2.17 void setSizeIncrement(int w, int h)
设置当前控件的尺寸增量宽为w,高为h。
2.18 QSize sizeIncrement() const
获取窗口尺寸增量。
2.19 void setContentsMargins(int left, int top, int right, int bottom)
(1)功能说明
设置控件窗口边框与内容的边距,包含左、上、右、下四个边距。边距被用于布局系统(边距设置只有在布局上才起效),也可能会被用于在子类中指定区域进行绘制。
(2)效果展示
(3)调用程序
QtGuiAppTest.ui
QtGuiAppTest.h
#pragma