其他分享
首页 > 其他分享> > QT左右弹窗控件

QT左右弹窗控件

作者:互联网

QT自定义控件(左右方向弹出控件)

自定义控件存在的意义 :QT 内置控件丑,而且满足不了有些实际开发的需求。所以需要编写自定义控件来完成一些特殊需求。

效果展示

在这里插入图片描述

  1. 界面说明 :点击右侧中间的按钮会从左边或者右边弹出一个窗体,这个窗体还可以自定义。
  2. 功能说明 :单击按钮窗体弹出,再次单击窗体消失
  3. 补充说明:弹窗可自定义成任意控件。亦可以设置按钮与弹窗之间的 距离
代码:

class myButton : public QToolButton
{
   Q_OBJECT
public:
   explicit myButton(QWidget *parent = nullptr);
    ~myButton();
   //myButton(QWidget *parent = nullptr,int pos=0);
public:
   void initWidgetInfo();
   void createTool(toolWidget*tools);
   void mousePressEvent(QMouseEvent *);
   void moveEvent(QMoveEvent *);
   void hideEvent(QHideEvent*);
   void setListParent(QWidget*parent);
   void setItemWidgetPos(int pos);
   void setSpacting(int spacing);
   void dealWithAnimation();
   void printPos();
signals:

public slots:
private:
  //QListWidget *m_list;
   toolWidget      *m_list;
   int m_pos;
   int m_spacing = 0;
};


myButton::myButton(QWidget *parent) : QToolButton(parent)
 ,m_list(NULL)
{
  //this->setListParent(parent);
  //this->setFixedSize(100,40);
  m_pos =1;
  m_spacing = 2;
 this->setStyleSheet("QWidget{background-color: rgb(233, 185, 110);color:green;border:3px solid gray;padding:3px 3px 3px 3px;}");

}
myButton::~myButton()
{
   if(m_list)
   {
       delete m_list;
       m_list = NULL;
   }

}
void myButton::createTool(toolWidget*tools)
{
   if(!tools)
   {
       return;
   }
   if(!m_list)
   {
       m_list = tools;

   }
   initWidgetInfo();
}
void myButton::initWidgetInfo()
{
   setItemWidgetPos(m_list->m_pos);
   if(m_list->m_type == 0) //
   {
      m_list->setFixedSize(100,150);
      connect(m_list,&toolWidget::SigListWidgetTextChanged,[=](QString text){
          this->setText(text);
      });
      connect(m_list,&toolWidget::SigListWidgetDoubleClicked,[=](){
          m_list->hide();
      });
   }
   else if(m_list->m_type == 1)
   {
       m_list->setFixedSize(360,160);
       connect(m_list,&toolWidget::SigMyCalendarOkBtnClicked,[=](QString date){
           this->setText(date);
           if(m_list->isVisible())
           {
               m_list->hide();
           }
       });
   }

   QPoint point =this->mapToGlobal(QPoint(0,0));
   m_list->move(point.x(),point.y());
   m_list->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);// 顶层无边框窗体
   m_list->setStyleSheet("QWidget{background-color: rgb(233, 185, 110);border:3px solid gray;padding:3px 3px 3px 3px;}"
                                               "QListWidget::item{color: rgb(78, 154, 6);height:20px;padding-left:5px;}"
                                               "QListWidget::item:hover{color: rgb(92, 53, 102);}"
                                               "QListWidget::item::selected:active{color:red;border-width:10;}"
                                               "QListWidget::item:selected{color:red;border-width:10;}"
                                               );


    m_list->hide();
}

void myButton::setItemWidgetPos(int pos)
{
  m_pos = pos;
}
void myButton::setSpacting(int spacing)
{
   m_spacing = spacing;
}
void myButton::hideEvent(QHideEvent *)
{
   if(m_list)
   {
       if(m_list->isVisible())
       {
           m_list->hide();
       }
   }
}
void myButton::moveEvent(QMoveEvent *)
{
  if(!m_list)
  {
      return;
  }
   int x = 0,y=0;
   QPoint point =this->mapToGlobal(QPoint(0,0));
   if(m_pos == 0)  // left
   {
      x= point.x()-this->width()-m_list->width()-m_spacing;

   }
   else //right
   {
       x = point.x()+m_spacing;
   }
   y = point.y()-m_list->height()/2+this->height()/2;
   m_list->move(this->width()+x,y);
   //m_list->move(0,0);
}
void myButton::setListParent(QWidget *parent)
{
   if(!m_list||!parent)
   {
       return;
   }
   m_list->setParent(parent);
}
void myButton::mousePressEvent(QMouseEvent *)
{
   if(!m_list)
   {
       return;
   }
   if(!m_list->isVisible())
   {
       int x = 0,y =0;
       QPoint point =this->mapToGlobal(QPoint(0,0));

       if(m_pos==0)
       {
           //x= this->x()-this->width()-m_list->width()-m_spacing;
           x = point.x()-m_list->width()-m_spacing;
           y = point.y()-m_list->height()/2+this->height()/2;
           m_list->move(x,y);

           m_list->show();
           QPropertyAnimation *animation= new QPropertyAnimation(m_list,"geometry");
           animation->setDuration(200);
           animation->setStartValue(QRect(m_list->pos().x()+m_list->width(),m_list->pos().y(),m_list->width(),m_list->height()));
           animation->setEndValue(QRect(m_list->pos().x(),m_list->pos().y(),m_list->width(),m_list->height()));
           animation->start();
       }
       else
       {
           x =point.x()+m_spacing+m_list->width();
           y = point.y()-m_list->height()/2+this->height()/2;
           m_list->move(x,y);
           m_list->show();
           QPropertyAnimation *animation= new QPropertyAnimation(m_list,"geometry");
           animation->setDuration(200);
           animation->setStartValue(QRect(x-m_list->width(),m_list->pos().y(),m_list->width(),m_list->height()));
           animation->setEndValue(QRect(x,m_list->pos().y(),m_list->width(),m_list->height()));
           animation->start();


       }
   }
   else
   {

       m_list->hide();

   }
}
void myButton::printPos()
{
   QPoint btnP= this->mapToGlobal(QPoint(0,0));
   QPoint listP= m_list->mapToGlobal(QPoint(0,0));
   qDebug()<<"this:"<<btnP.x()<<"   "<<btnP.y();
   qDebug()<<"list:"<<listP.x()<<"  "<<listP.y();
}

标签:控件,QT,point,void,list,pos,myButton,width,弹窗
来源: https://blog.csdn.net/qq_22878985/article/details/115449194