Qt实现可拖动按钮
程序员文章站
2022-03-16 17:48:34
本文实例为大家分享了qt实现可拖动按钮的具体代码,供大家参考,具体内容如下直接上代码self-contained.h#ifndef selfcontained_h#define selfcontain...
本文实例为大家分享了qt实现可拖动按钮的具体代码,供大家参考,具体内容如下
直接上代码
self-contained.h
#ifndef selfcontained_h #define selfcontained_h #include <qwidget> #include <qpainter> #include <qtimer> #include <qimage> #include <qmouseevent> #include <qvector> #endif // selfcontained_h
按钮控件
drawbutton.h:
#ifndef dragbutton_h #define dragbutton_h #include "self-contained.h" class dragbutton : public qwidget { q_object public: dragbutton(qwidget *parent = 0); void setinitialscaling(double scaling); void setpixmap(qstring pixmap); void settext(qstring str); void setishold(bool flag); protected: int ispress; int ishold; qtimer *m_anitimer; qtimer *m_holdtimer; double m_scaling; double m_initialscaling;//当前缩放比例 qpoint m_mousesrcpos;//最小缩放比例 qpixmap m_pixmap; qstring m_text; void paintevent(qpaintevent *); void enterevent(qevent *); void leaveevent(qevent *); void mousepressevent(qmouseevent *); void mousereleaseevent(qmouseevent *); void mousemoveevent(qmouseevent *); signals: void release_signal(); void drag_signal();//拖动时发出信号 void clicked(); public slots: void zoomin();//放大 void zoomout();//缩小 void hold_slot(); }; #endif // dragbutton_h
drawbutton.cpp
#include "dragbutton.h" dragbutton::dragbutton(qwidget *parent) : qwidget(parent),ispress(0),ishold(0),m_scaling(0.5),m_initialscaling(0.5),m_mousesrcpos(0,0) { m_anitimer = new qtimer(this); m_anitimer->setinterval(7); m_holdtimer = new qtimer(this); m_holdtimer->setinterval(1000); m_holdtimer->setsingleshot(true); connect(m_holdtimer,signal(timeout()),this,slot(hold_slot())); } void dragbutton::setinitialscaling(double scaling) { if(scaling <= 1 && scaling > 0) { m_initialscaling = scaling; m_scaling = scaling; } } void dragbutton::setpixmap(qstring pixmap) { m_pixmap.load(pixmap); update(); } void dragbutton::settext(qstring str) { m_text = str; update(); } void dragbutton::setishold(bool flag) { ishold = flag; update(); } void dragbutton::paintevent(qpaintevent *) { if(m_pixmap.isnull()) return; qpainter painter(this); painter.setrenderhint(qpainter::antialiasing); if(ispress) { painter.setpen(qt::nopen); painter.setbrush(qcolor(0,0,0,130)); painter.drawroundedrect(rect(),20,20); } m_pixmap = m_pixmap.scaled(width(),height() - 20,qt::keepaspectratio,qt::smoothtransformation); int w = m_pixmap.width()*m_scaling; int h = m_pixmap.height()*m_scaling; painter.drawpixmap(qrect((width() - w)/2,(height() - h)/2 - 20,w,h), m_pixmap,m_pixmap.rect()); painter.setpen(qcolor(0,0,0)); painter.drawtext(qrect(0,height() - 40,width(),40),qt::aligncenter,m_text); } void dragbutton::enterevent(qevent *) { disconnect(m_anitimer,signal(timeout()),this,slot(zoomout())); connect(m_anitimer,signal(timeout()),this,slot(zoomin())); m_anitimer->start(); } void dragbutton::leaveevent(qevent *) { disconnect(m_anitimer,signal(timeout()),this,slot(zoomin())); connect(m_anitimer,signal(timeout()),this,slot(zoomout())); m_anitimer->start(); } void dragbutton::mousepressevent(qmouseevent *e) { if(!ishold) ispress = 1; m_holdtimer->start(); m_mousesrcpos = e->pos(); update(); } void dragbutton::mousereleaseevent(qmouseevent *e) { m_holdtimer->stop(); ispress = 0; ishold = 0; if(rect().contains(e->pos())) emit clicked(); emit release_signal(); update(); } void dragbutton::mousemoveevent(qmouseevent *e) { if(ishold) { move(pos() - m_mousesrcpos + e->pos()); emit drag_signal(); } else m_mousesrcpos = e->pos(); } void dragbutton::zoomin() { m_scaling += 0.01; if(m_scaling >= 1) { m_scaling = 1; m_anitimer->stop(); } update(); } void dragbutton::zoomout() { m_scaling -= 0.01; if(m_scaling <= m_initialscaling) { m_scaling = m_initialscaling; m_anitimer->stop(); } update(); } void dragbutton::hold_slot() { ishold = 1; ispress = 0; m_anitimer->stop(); m_scaling = m_initialscaling; update(); }
整合按钮的控件
drawwidget.h
#include "dragbutton.h" class dragwidget : public qwidget { q_object public: dragwidget(qwidget *parent = 0); ~dragwidget(); void addbutton(dragbutton*); protected: qvector<dragbutton*> btnvector; qpoint m_mousesrcpos;//记录坐标点 void resetinterface();//复位 signals: public slots: void btnmove_slots(); void btnrelease_slots(); }; #endif // dragwidget_h
drawwidget.cpp
#include "dragwidget.h" dragwidget::dragwidget(qwidget *parent) : qwidget(parent),m_mousesrcpos(0,0) { } dragwidget::~dragwidget() { } void dragwidget::addbutton(dragbutton* btn) { connect(btn,signal(drag_signal()),this,slot(btnmove_slots())); connect(btn,signal(release_signal()),this,slot(btnrelease_slots())); btnvector.push_back(btn); btn->show(); resetinterface(); } void dragwidget::resetinterface() { for(int i = 0;i < btnvector.length();++i) { btnvector[i]->setgeometry(i * width()/btnvector.length(),0,width()/btnvector.length(),height()); } } void dragwidget::btnmove_slots() { for(int i = 0;i < btnvector.length();++i)//找到鼠标所在的按钮 if(btnvector[i] == sender()) { int flag = (btnvector[i]->pos().x() + btnvector[i]->width()/2)/(width()/btnvector.length()); for(int l = 0;l < btnvector.length();++l)//这里也可以做动画,但这次主要实现拖动的功能 { if(l < i && l <flag) btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); else if((l > i && l <= flag)||(l >= flag && l < i)) btnvector[l]->setgeometry((l + ((i-flag)>0?1:-1))* width()/btnvector.length(),0,width()/btnvector.length(),height()); else if(l > flag && l > i) btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); } //注释部分合为上面的循环 // if(flag >= i)//往后拖 // for(int l = 0;l < btnvector.length();++l) // { // if(l < i) // btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); // else if(l > i && l <= flag) // btnvector[l]->setgeometry((l - 1)* width()/btnvector.length(),0,width()/btnvector.length(),height()); // else if(l > flag) // btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); // } // else if(flag < i)//往前拖 // for(int l = 0;l < btnvector.length();++l) // { // if(l < flag) // btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); // else if(l >= flag && l < i) // btnvector[l]->setgeometry((l + 1)* width()/btnvector.length(),0,width()/btnvector.length(),height()); // else if(l > i) // btnvector[l]->setgeometry(l * width()/btnvector.length(),0,width()/btnvector.length(),height()); // } break; } } void dragwidget::btnrelease_slots() { for(int i = 0;i < btnvector.length();++i)//找到鼠标所在的按钮 if(btnvector[i] == sender()) { int posx = btnvector[i]->pos().x(); if(posx < 0) posx = 0; else if(posx > width()) posx = width(); int flag = (posx+btnvector[i]->width()/2)/(width()/btnvector.length()); dragbutton *btn = btnvector[i];//修改vector顺序 if(flag >= i) for(int l = i;l < flag;++l) btnvector[l] = btnvector[l+1]; else for(int l = i;l > flag;--l) btnvector[l] = btnvector[l-1]; btnvector[flag] = btn; } resetinterface();//复位 }
使用
main.cpp
#include "dragwidget.h" #include <qapplication> int main(int argc, char *argv[]) { qapplication a(argc, argv); dragwidget ww; ww.setgeometry(200,200,800,200); dragbutton w(&ww); w.setpixmap(":/image/image/contacts.png"); w.settext("按钮1"); w.setinitialscaling(0.6); dragbutton w2(&ww); w2.setpixmap(":/image/image/time.png"); w2.settext("按钮2"); w2.setinitialscaling(0.6); dragbutton w3(&ww); w3.setpixmap(":/image/image/checking.png"); w3.settext("按钮3"); w3.setinitialscaling(0.6); dragbutton w4(&ww); w4.setpixmap(":/image/image/suitcase.png"); w4.settext("按钮4"); w4.setinitialscaling(0.6); ww.addbutton(&w); ww.addbutton(&w2); ww.addbutton(&w3); ww.addbutton(&w4); ww.show(); return a.exec(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。