Qt实现iphone小白点

按钮类
.h
class SmartpushButton : public QPushButton
{
    Q_OBJECT

public:
    explicit SmartpushButton(QPushButton *parent = 0);

protected:
    //鼠标按下事件
    void mousePressEvent(QMouseEvent *event);
   //鼠标释放事件
    void mouseReleaseEvent(QMouseEvent *event);
   //鼠标移动事件
    void mouseMoveEvent(QMouseEvent *event);
private:
    QPoint move_point; //移动的距离
    bool mouse_press; //鼠标按下
    bool button_moveed;//按钮移动过
public slots:

signals:
    void buttonclicked();

};
.cpp
SmartpushButton::SmartpushButton(QPushButton *parent) :
    QPushButton(parent)
{
    mouse_press = false;
    this->setMouseTracking(true);
    this->setWindowFlags(Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
    this -> setFlat(true);

    this->setToolTip(tr("工具箱"));
    QPixmap *pixmap_smart = new QPixmap(100, 100);
    pixmap_smart->load(":/image/smartbutton2.png");
    QIcon *icon_smart = new QIcon(*pixmap_smart);
    this->setIconSize(QSize(80, 80));
    this->setIcon(*icon_smart);
    this->setFixedSize(35,33);
    this->setStyleSheet("border:2px groove gray;border-radius:5;padding:2px 4px;");
    this->setAttribute(Qt::WA_TranslucentBackground, true);
}
void SmartpushButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        mouse_press = true;
        //鼠标相对于窗体的位置(或者使用event->globalPos() - this->pos())
        move_point = event->pos();
        button_moveed = false;

    }

}

void SmartpushButton::mouseMoveEvent(QMouseEvent *event)
{
    //若鼠标左键被按下
    if(mouse_press)
    {
       //鼠标相对于屏幕的位置
       QPoint move_pos = event->globalPos();
       //移动按钮位置
       this->move(move_pos - move_point);
       button_moveed = true;
    }
}

void SmartpushButton::mouseReleaseEvent(QMouseEvent *event)
{
    //设置鼠标为未被按下
    mouse_press = false;
    if(!button_moveed)
    {
        emit(buttonclicked());
    }
}

上面这个按钮类主要实现按钮能够实现拖拽

在主函数中
connect(smarttoolbutton,SIGNAL(buttonclicked()),this,SLOT(smarttoolbuttonclicked()));
void MainWindow::smarttoolbuttonclicked()
{
    const int Toolcontainerwith = 150;
    const int widthMargin = 100;
    if(smarttoolbutton->pos().x() > QApplication::desktop()->width()-(Toolcontainerwith+widthMargin))
    {
        Toolcontainerdlg->setGeometry(smarttoolbutton->pos().x()-Toolcontainerwith-3,smarttoolbutton->pos().y()-3,Toolcontainerwith,40);
    }
    else
    {
        Toolcontainerdlg->setGeometry(smarttoolbutton->pos().x()+smarttoolbutton->width()+3,smarttoolbutton->pos().y()-3,Toolcontainerwith,40);
    }
    Toolcontainerdlg->show();
}

Toolcontainerdlg 是一个dialog,相关操作如下:
初始化:

    Toolcontainerdlg = new QDialog(this);
    Toolcontainerdlg->setLayout(TooldlgcontainerGridlayout);
    Toolcontainerdlg->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint);
    Toolcontainerdlg->setWindowOpacity(0.8);
    Toolcontainerdlg->setStyleSheet("background-color: rgb(48, 48, 48);");

接下来往小白点上添加工具按钮:

/*tool 1*/
    {
        toolbox1 = new QVBoxLayout();
        QPixmap *pixmap = new QPixmap(200, 150);
        pixmap->load(":/image/scan.png");
        QIcon *icon = new QIcon(*pixmap);

        scanChannelbutton = new QPushButton(*icon, "", Toolcontainerdlg);
        scanChannelbutton->setIconSize(QSize(40, 40));
        scanChannelbutton->setFixedSize(40, 40);
        scanChannelbutton->setToolTip(tr("全信道扫描工具"));

        QLabel *scanChannelLabel = new QLabel();
        scanChannelLabel->setText("信道扫描");
        scanChannelLabel->setFont(QFont("Times", 9, QFont::Bold));
        scanChannelLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
        scanChannelLabel->setStyleSheet("color: rgb(255, 255, 255);");

        toolbox1->addWidget(scanChannelbutton,0,Qt::AlignHCenter);
        toolbox1->addWidget(scanChannelLabel,1,Qt::AlignHCenter);
        connect(scanChannelbutton,SIGNAL(clicked()),this,SLOT(on_scanchannel_button_clicked()));
    }
/*tool 2*/
    {
        toolbox2 = new QVBoxLayout();
        QPixmap *pixmap = new QPixmap(200, 150);
        pixmap->load(":/image/importdatatool.png");
        QIcon *icon = new QIcon(*pixmap);

        importDatabutton = new QPushButton(*icon, "", Toolcontainerdlg);
        importDatabutton->setIconSize(QSize(40, 40));
        importDatabutton->setFixedSize(40, 40);
        importDatabutton->setToolTip(tr("导入数据库工具"));

        QLabel *importDataLabel = new QLabel();
        importDataLabel->setText("导入数据库");
        importDataLabel->setFont(QFont("Times", 9, QFont::Bold));
        importDataLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
        importDataLabel->setStyleSheet("color: rgb(255, 255, 255);");

        toolbox2->addWidget(importDatabutton,0,Qt::AlignHCenter);
        toolbox2->addWidget(importDataLabel,1,Qt::AlignHCenter);
        connect(importDatabutton,SIGNAL(clicked()),this,SLOT(on_importdata_button_clicked()));
    }

效果如下:
外观

这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值