搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
你好赵伟
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
【OpenCV】第十九章: 视频操作入门_cv::videocapture
2
Android源码下载网址
3
用批处理命令打开控制面板选项_批处理打开控制面板
4
获取linux内存、cpu、磁盘IO等信息脚本及其原理详解_shell 脚本 cpu 取平均值
5
anaconda目录下的pkgs文件夹很大,可以删除吗?_anaconda pkgs
6
Yolov5、yolov7、yolov8训练自己的数据集(不用配环境,只需3步就能训练自己的数据集)_yolov7 百度网盘
7
sklearn.metrics 中多分类问题指标的计算函数使用_y_score怎么计算
8
oracleshell执行新医保数据转换后台_oracle 医保
9
GPT2原理_gpt2基础原理
10
【SPIE独立出版 | Ei检索 】第二届物联网与机器学习国际学术会议征稿中!_spie会议论文集影响因子
当前位置:
article
> 正文
Qt自定义窗口部件_qt geometry修改中心
作者:你好赵伟 | 2024-02-19 13:54:54
赞
踩
qt geometry修改中心
Qt自定义窗口部件
QtDesigner自定义窗口部件有两种方法:改进法(promotion)和插件法(plugin)
改进法
1、改进法之前,要先写好子类化QSpinBox后
Qt自定义窗口部件
的HexspinBox.h和HexspinBox.cpp文件。把这两个文件拷贝到想要的项目中。
HexspinBox.h
Cpp代码
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QSpinBox>
class
QRegExpValidator;
class
HexSpinBox :
public
QSpinBox
{
Q_OBJECT
public
:
HexSpinBox(QWidget *parent = 0);
protected
:
QValidator::State validate(QString &text,
int
&pos)
const
;
int
valueFromText(
const
QString &text)
const
;
QString textFromValue(
int
value)
const
;
private
:
QRegExpValidator *validator;
};
#endif
HexspinBox.cpp
Cpp代码
#include <QtGui>
#include "hexspinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent)
: QSpinBox(parent)
{
setRange(0, 255);
validator =
new
QRegExpValidator(QRegExp(
"[0-9A-Fa-f]{1,8}"
),
this
);
}
QValidator::State HexSpinBox::validate(QString &text,
int
&pos)
const
{
return
validator->validate(text, pos);
}
int
HexSpinBox::valueFromText(
const
QString &text)
const
{
bool
ok;
return
text.toInt(&ok, 16);
}
QString HexSpinBox::textFromValue(
int
value)
const
{
return
QString::number(value, 16).toUpper();
}
2、在需要开发的项目中的窗口中,
1、用Qt Designer创建一个新的窗体main.ui,把控件箱里的QSpinBox添加到窗体中。
2、右击微调框,选择“Promote to ”上下文菜单。
3、在弹出的对话框中,类名处填写“HexSpinBox”,头文件填写“hexspinbox.h”
好了。在ui生成的包含有QSpinBox的控件文件中,ui的源代码里面多了一段
<customwidgets>
<customwidget>
<class>HSpinBox</class>
<extends>QSpinBox</extends>
<header>hspinbox.h</header>
</customwidget>
包含文件变为"hexspinbox.h"。在Qt Designer中,QSpinBox表示的控件为HexSpinBox,并且可以设置所有的QSpinBox的属性。
可以在VS2008中编译一下main.ui文件,从ui_main.h源代码中可以知道,引入的控件是:
Cpp代码
#include <QtGui/QTableWidget>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
#include "hspinbox.h"
QT_BEGIN_NAMESPACE
class
Ui_QMainClass
{
public
:
QWidget *centralWidget;
QPushButton *pushButton;
QTableWidget *tableWidget;
QSpinBox *spinBox;
HSpinBox *hspinBox;
升级法的缺点是不能在Qt Designer中设置自定义控件自己的特有属性,也不能够绘制自己。这些问题可以用插件法解决。
插件法
1.VS中创建Qt4 Design Plugin 工程 ,名称叫custom
自动建立如下几个文件:
自定义控件:custom.h,custom.cpp
插件:customplugin.h,customplugin.cpp
源代码如下:
custom.h
Cpp代码
#ifndef CUSTOM_H
#define CUSTOM_H
#include <QtGui/QWidget>
#include "ui_test.h"
class
custom :
public
QWidget
{
Q_OBJECT
public
:
custom(QWidget *parent = 0);
~custom();
private
:
Ui::Form ui;
};
#endif // CUSTOM_H
custom.cpp
Cpp代码
#include "custom.h"
custom::custom(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(
this
);
}
custom::~custom()
{
}
customplugin.h
Cpp代码
#ifndef CUSTOMPLUGIN_H
#define CUSTOMPLUGIN_H
#include <QDesignerCustomWidgetInterface>
class
customPlugin :
public
QObject,
public
QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
public
:
customPlugin(QObject *parent = 0);
bool
isContainer()
const
;
bool
isInitialized()
const
;
QIcon icon()
const
;
QString domXml()
const
;
QString group()
const
;
QString includeFile()
const
;
QString name()
const
;
QString toolTip()
const
;
QString whatsThis()
const
;
QWidget *createWidget(QWidget *parent);
void
initialize(QDesignerFormEditorInterface *core);
private
:
bool
initialized;
};
#endif // CUSTOMPLUGIN_H
customplugin.cpp
Cpp代码
#include "custom.h"
#include <QtCore/QtPlugin>
#include "customplugin.h"
customPlugin::customPlugin(QObject *parent)
: QObject(parent)
{
initialized =
false
;
}
void
customPlugin::initialize(QDesignerFormEditorInterface *
/*core*/
)
{
if
(initialized)
return
;
initialized =
true
;
}
bool
customPlugin::isInitialized()
const
{
return
initialized;
}
QWidget *customPlugin::createWidget(QWidget *parent)
{
return
new
custom(parent);
}
QString customPlugin::name()
const
{
return
"custom"
;
}
QString customPlugin::group()
const
{
return
"My Plugins"
;
}
QIcon customPlugin::icon()
const
{
return
QIcon();
}
QString customPlugin::toolTip()
const
{
return
QString();
}
QString customPlugin::whatsThis()
const
{
return
QString();
}
bool
customPlugin::isContainer()
const
{
return
false
;
}
QString customPlugin::domXml()
const
{
return
"<widget class=\"custom\" name=\"custom\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>100</width>\n"
" <height>100</height>\n"
" </rect>\n"
" </property>\n"
"</widget>\n"
;
}
QString customPlugin::includeFile()
const
{
return
"custom.h"
;
}
Q_EXPORT_PLUGIN2(custom, customPlugin)
在其cpp的最后必须 添加下面的宏:
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
Q_EXPORT_PLUGIN2(customWidgetPlugin, CustomWidgetPlugin)
// 第一个参数为插件的名字,第二个是插件类的名字(而不是自定义控件的类名)
2.
新建后,直接编译,会产生如下错误
1>LINK : fatal error LNK1181: cannot open input file 'QtDesignerd.lib'
这是因为此工程默认引用的是QtDesignerd.lib库,更改其为版本对应的库即可消除故障(VS2008是在项目的属性中Linker/input/Additional Dependencies中修改,我这里Debug配置使用的是QtDesignerd4.lib,Release版本使用QtDesigner4.lib)。
3、使用自定义插件
1)、
只需要把通过Release模式生成的
项目.lib和项目.dll文件拷到C:\Qt\4.7.4\plugins\designer中,
2)、 然后在QtDesigner中,选择菜单Help/About Plugin就可以看到你的自定义控件是否已经载入成功。
在QtDesigner中控件列表中有一项My Widget 中就有你的自定义控件。
参考:
1、
Qt自定义控件(插件)并添加到QtDesigher
打开QtDesigner,我们自定义的空间custom成功使用界面如下:
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/你好赵伟/article/detail/115078?site
推荐阅读
article
el
-
s
el
ect
数据
量过大引发卡顿,怎么办?_
el
-
s
el
ect
选择器
数据
很多的话 筛选很卡...
经典问题:在测试环境好好的,怎么到正式环境就不行了?_
el
-
s
el
ect
选择器
数据
很多的话 筛选很卡
el
-
s
el
ect
选...
赞
踩
article
GraspNet
-
1Billion
中
graspnet
结构
_
graspnet
对比...
GraspNet
-
1Billion
中
graspnet
结构
_
graspnet
对比
graspnet
对比 ...
赞
踩
article
AntD
中
Form
几种
获取
数据
的方式_
antd
中
form
表单
如何
获取
值...
在
Form
.item不单独绑定value 值和onChange方法时使用1、Button在
Form
内部,加上 htmlT...
赞
踩
article
一文了解
Web3.0
真实
社交
先驱
ERA
...
ERA
兼具
社交
的裂变流量和加密钱包的Web3入口流量,再结合其对去中心化身份的真人解决方案,将有望开创Web3
真实
社交
的...
赞
踩
article
Vue2
- 模板、
渲染
、
事件处理
、表单输入
绑定
_
html
vue2
表单...
如果数据项的顺序被改变,Vue 将不会移动 DOM 元素来匹配数据项的顺序,而是就地更新每个元素,并且确保它们在每个索引...
赞
踩
article
vue+
el
ement
获取
el
-
table
某行中的
下标
,根据
下标
操作
数组
对象_
el
ement
tab ...
1.在vue中对
数组
中的某个对象进行操作时(替换、删除),都需要用到该对象在
数组
中的
下标
。前端代码:scope.$ind...
赞
踩
article
php
基础学习之
匿名
函数
...
允许临时创建一个没有指定名称的
函数
,使用
匿名
函数
可以方便地创建一次性或临时的功能块。
php
基础学习之
匿名
函数
...
赞
踩
article
input
获取焦点后不
显示
光标
,能
输入
显示
内容
_
input
没有
光标
...
在开发的过程中会遇到各种各样的奇葩需求,这就遇到一个,
input
输入
框
输入
的时候不要
显示
光标
。不得不说这个需求太奇葩了,...
赞
踩
article
C#-----
类型
转换
、
变量
类型
、
异常
处理_c# 子类
转换
为 接口类 时 抛出
异常
...
类型
转换
隐式
转换
:任何
类型
A,只要取值范围完全包含在
类型
B的取值范围内,就可以隐式
转换
为
类型
B。显示
转换
:在明确要求编译...
赞
踩
article
如何3分钟在
Windows
11
上启用
Copilot
_
win
11
copilot
怎么
开启
...
Windows
Copilot
是
Windows
11
中的一个新功能,它可以让你与一个智能助理进行对话,获取信息,执...
赞
踩
article
安装
graspnet
的方法_
graspnet
api
安装
...
nvidia3060下。_
graspnet
api
安装
graspnet
api
安装
nv...
赞
踩
article
3D
GS
谁敢
一战
?
26ms
完成
三维重建
!...
来源:
3D
视觉工坊在公众号「
3D
视觉工坊」后台,回复「原论文」可获取论文pdf、代码、官方主页链接这里给大家推荐下Gau...
赞
踩
article
使用
ant
-
design
-
vue
的
Form
表单_
vue
design
,
form
示例...
文章目录使用脚手架新建项目安装并导入
ant
-
design
-
vue
,使用
Form
组件修改main.ts修改App.
vue
修...
赞
踩
article
【
MySQL
】Error Code: 2013.
Lost
connection
to
MySQL
...
本文只记录了本人成功解决错误的一种方法。_
error
code
2013
lost
error
code
2013 los...
赞
踩
article
umy
-
ui
——
table
检索字段自动滚到指定位置并
高亮
_
umy
-
ui
table
...
通过input输入框,输入要查找的数据字段,点击确定可以定位到查找的那行数据、并把改行显示
高亮
_
umy
-
ui
table
...
赞
踩
article
vue
实现
el
-
s
el
ect
下拉选项的
懒
加载
_
el
ement-ui
image
图片放到了
el
...
下拉选择是常用的用户交互选择的操作;常用固定选择项或者动态渲染选择项.实际项目中存在数据量大,一次性渲染很多数据会造成下...
赞
踩
article
Qt
助手
(
assistant
):方便
查找
Qt
类...
用
Qt
助手
查找
Qt
类_qt
助手
qt
助手
一个方便
查找
QT类用法的...
赞
踩
article
React
-
class
——
Redux
、
ant
Design
_
react
、reducx、
ant
关...
在标准的MVC框架中,数据可以在UI组件和存储之间双向流动,而
Redux
严格限制了数据只能在一个方向上流动。2.2、ac...
赞
踩
article
【Vue】
el
-
table
取消
鼠标
悬浮时行高亮效果_
vue
table
关闭
hover
...
el
-
table
组件,
鼠标
悬停时会有默认事件:高亮当前行;可以把颜色设置成背景色,或者任何自己想要的颜色:div /de...
赞
踩
article
>
箭头>
>
函数>()
>
=>
>
{}与
>
function>的区别_(()
>
=>
>
{...
1.
>
箭头>
>
函数>与
>
function>定义
>
函数>的写法://
>
function>
>
function> fn(a, b){ return a...
赞
踩
相关标签
elementui
vue.js
前端
el-select
数据量
python
人工智能
深度学习
网络
javascript
react.js
web3
vue
渲染
php
学习笔记
windows
copilot
ubuntu
linux
服务器
3d
mysql
数据库