如何用Qt实现组件供QML使用

如何用Qt实现组件供QML使用,第1张

用Qt实现一个UI:一个圆形图标圆圈内或圆圈上拖动,但不能拖出到圆圈外。当拖动到圆圈上时,高亮图标和圆圈。类似有RingLock。

1、继承QQuickPaintedItem类,该类为QQuickItem的子类。QQuickItem用于不用显示UI的供QML使用的组件;QQuickPaintedItem用于需要显示UI的供QML使用的组件。本案例中,需要画图,故而继承QQuickPaintedItem。

/imagedragwidgeth/

#ifndef IMAGEDRAGWIDGET_H

#define IMAGEDRAGWIDGET_H

#include <QQuickPaintedItem>

#include <QtCore>

#include <QtGui>

class imageDragWidget : public QQuickPaintedItem

{

Q_OBJECT

public:

explicit imageDragWidget(QQuickPaintedItem parent = 0);

~imageDragWidget();

signals:

//鼠标按下

void dragPress();

//鼠标在圆圈内移动

void dragMoveIn();

//鼠标在圆圈上移动

void dragMoveOn();

//鼠标释放

void dragRelease();

//鼠标移出圆圈,确认关机

void dragOut();

public slots:

protected:

void paint(QPainter painter);

void mouseMoveEvent(QMouseEvent event);

void mousePressEvent(QMouseEvent event);

void mouseReleaseEvent(QMouseEvent event);

private:

//判断鼠标和圆圈的位置关系:圆圈外、圆圈上、圆圈内

int circleContain(void);

//判断鼠标和图标的位置关系:图标外、图标上、图标内

int powerContain(void);

//得到鼠标与圆心连线和圆圈的交点

QPoint GetPoint(QPoint currentPoint, QPoint circleCenter, int raduis);

private:

QPixmap circle_defaultImg;

QPixmap circle_boldImg;

QPixmap power_haloImg;

QPixmap power_solidImg;

QPixmap power_defaultImg;

//当前圆圈

QPixmap circleImg;

//圆圈所在矩形

QRect circleImgRect;

//当前图标

QPixmap powerImg;

//图标所在矩形

QRect powerImgRect;

//当前鼠标所在位置

QPoint currentMousePoint;

//图标所在位置

QPoint powerCenterPoint;

//鼠标是否按下的标志

bool pressFlag;

//鼠标是否移出的标志

bool isOut;

//宽度缩放比例

double widthScale;

//高度缩放比例

double heightScale;

};

#endif // IMAGEDRAGWIDGET_H

/imagedragwidgetcpp/

#include "imagedragwidgeth"

#include <QDebug>

imageDragWidget::imageDragWidget(QQuickPaintedItem parent) :

QQuickPaintedItem(parent)

{

//得到屏幕尺寸

QScreen screen = QGuiApplication::primaryScreen();

int screen_width = screen->size()width();

int screen_height = screen->size()height();

qDebug()<<"屏幕尺寸: "<<screen_width<<""<<screen_height;

//圆圈所在尺寸为:452452; 图标所在尺寸为:350350

//滑动图标的尺寸128128

double widgetScale = (double)580/(double)720;

qDebug()<<"控件占屏幕比例: "<<widgetScale;

//设置控件尺寸

setContentsSize(QSize(screen_widthwidgetScale, screen_widthwidgetScale));

int widget_width = contentsSize()width();

int widget_height = contentsSize()height();

qDebug()<<"控件尺寸: "<<widget_width<<""<<widget_height;

//接收鼠标左键

setAcceptedMouseButtons(Qt::LeftButton);

circle_defaultImg = new QPixmap(":/images/circle_defaultpng");

circle_boldImg = new QPixmap(":/images/circle_boldpng");

power_haloImg = new QPixmap(":/images/power_halopng");;

power_solidImg = new QPixmap(":/images/power_solidpng");

power_defaultImg = new QPixmap(":/images/power_defaultpng");

isOut = false;

circleImg = circle_defaultImg;

int circle_width = circleImg->width();

int circle_height = circleImg->height();

//设置圆圈在实际屏幕上的尺寸

//滑动图标的尺寸128128

int circle_width_in_widget = widget_width - 128widgetScale;

int circle_height_in_widget = widget_height - 128widgetScale;

qDebug()<<"滑动圆圈尺寸: "<<circle_width_in_widget<<""<<circle_height_in_widget;

widthScale = (double)circle_width_in_widget/(double)circle_width;

heightScale = (double)circle_height_in_widget/(double)circle_height;

qDebug()<<"圆圈和图标宽度缩放比例为: "<<widthScale<<"高度缩放比例为: "<<heightScale;

circleImgRect = new QRect(0, 0, circle_widthwidthScale, circle_heightheightScale);

//圆圈移到控件中心

circleImgRect->moveCenter(QPoint(widget_width/2, widget_height/2));

powerImg = power_defaultImg;

int power_width = powerImg->width();

int power_height = powerImg->height();

powerImgRect = new QRect(0, 0, power_widthwidthScale, power_heightheightScale);

//图标移到控件中心

powerImgRect->moveCenter(circleImgRect->center());

powerCenterPoint = circleImgRect->center();

}

void imageDragWidget::paint(QPainter painter)

{

painter->drawPixmap(circleImgRect, circleImg);

painter->drawPixmap(powerImgRect, powerImg);

}

void imageDragWidget::mouseMoveEvent(QMouseEvent event)

{

if(pressFlag) {

//鼠标已按下

int power_width = powerImgRect->width();

int power_height = powerImgRect->height();

int circle_width = circleImgRect->width();

int circle_height = circleImgRect->height();

currentMousePoint = event->pos();

int flag = circleContain();

if(flag < 0) {

//鼠标在圆圈内,则图标移动到鼠标位置

powerImg = power_haloImg;

circleImg = circle_defaultImg;

powerImgRect->moveCenter(currentMousePoint);

powerCenterPoint = currentMousePoint;

isOut = false;

} else if(flag == 0) {

//鼠标在圆圈上,则图标移动到鼠标位置,同时更换

powerImg = power_solidImg;

circleImg = circle_boldImg;

powerImgRect->moveCenter(currentMousePoint);

powerCenterPoint = currentMousePoint;

isOut = true;

} else {

//鼠标在圆圈外

isOut = true;

if(powerContain() > 0) {

//鼠标在圆圈外且在图标外,则等同于鼠标释放。图标回到控件中心。

powerImg = power_defaultImg;

powerImgRect->moveCenter(circleImgRect->center());

pressFlag = false;

circleImg = circle_defaultImg;

} else {

//鼠标在圆圈外且不在图标外,则图标移到鼠标与控件中心连线和圆圈的交点。

powerImg = power_solidImg;

circleImg = circle_boldImg;

powerCenterPoint = GetPoint(currentMousePoint,

circleImgRect->center(), circleImgRect->width()/2);

powerImgRect->moveCenter(powerCenterPoint);

}

}

powerImgRect->setHeight(power_height);

powerImgRect->setWidth(power_width);

circleImgRect->setHeight(circle_height);

circleImgRect->setWidth(circle_width);

update();

if(pressFlag&&(!isOut)) {

//鼠标按下且在圆圈内

emit dragMoveIn();

} else if(pressFlag&&isOut){

//鼠标按下且在圆圈上

emit dragMoveOn();

} else if((!pressFlag)&&(isOut)){

//鼠标在圆圈外且在图标外,则等同于鼠标释放。

emit dragRelease();

}

if(isOut&&(!pressFlag)) {

//鼠标在圆圈外且在图标外,确认关机。

emit dragOut();

}

}

}

void imageDragWidget::mousePressEvent(QMouseEvent event)

{

currentMousePoint = event->pos();

if(powerContain() <= 0) {

//鼠标进入到图标内,则表示按下

pressFlag = true;

int power_width = powerImgRect->width();

int power_height = powerImgRect->height();

powerImg = power_haloImg;

powerImgRect->setHeight(power_height);

powerImgRect->setWidth(power_width);

update();

emit dragPress();

}

}

DLL命令 取坐标颜色, 整数型, "gdi32dll", "GetPixel"

参数 设备场景, 整数型

参数 x坐标, 整数型

参数 y坐标, 整数型

DLL命令 取设备场景, 整数型, "user32dll", "GetDC", , 获取指定窗口的设备场景,出错则为0

参数 窗口的句柄, 整数型, , 将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC

版本 2

子程序 _时钟1_周期事件

局部变量 颜色值, 整数型

局部变量 红, 整数型

局部变量 绿, 整数型

局部变量 蓝, 整数型

颜色值 = 取坐标颜色 (取设备场景 (0), 取鼠标水平位置 (), 取鼠标垂直位置 ())

编辑框1内容 = 到文本 (颜色值)

function dashboard_widget_output() { // 这里打印出你要输出的信息 } function dashboard_widget_setup() { wp_add_dashboard_widget( 'dashboard_widgt_id', '挂件标题', 'dashboard_widget_output' ); } add_action('wp_dashboard_setup', 'dashboard_widget_setup');上面的代码中,第一个函数,dashboard_widget_output()的作用是打印你要在dashboard中显示的信息,例如要显示当前服务器的PHP信息:function dashboard_widget_output() { echo '<p>'; phpinfo(); echo '</p>'; }第二个函数,dashboard_widget_setup()的作用就是对这个挂件进行必要的设置:wp_add_dashboard_widget( '这里填写挂件的id,命名同php函数命名吧', '挂件显示的标题', '打印函数,如dashboard_widget_output' );最后用add_action添加一个行为:add_action('wp_dashboard_setup', '设置函数,也就是第二个函数dashboard_widget_setup');就是这么简单的两步就可以在Dashboard上添加一个自定义的挂件了。

以上就是关于如何用Qt实现组件供QML使用全部的内容,包括:如何用Qt实现组件供QML使用、怎么获取指定坐标的颜色值、如何在桌面上使用Dashboard里的Widget乃等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9560144.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存