• Qt 自定义按钮


    自定义控件的实现思路如下:

    a1.新建一个类,该类继承QPushbutton,由于QPushbutton继承于QWidget,因此可以直接在该继承类里面进行布局管理和挂载控件;

    a2.新建两个QLabel实例,即buttonImage和buttonTxt(是QLable实例)。分别用两个垂直布局管理器QVBoxLayout挂载,即topLayout和bottomLayout(QVBoxLayout的实例)挂载。

    a3.然后新建一个垂直布局管理器mainLayout(QVBoxLayout的实例),用mainLayout将上面的两个topLayout和bottomLayout挂载进来。

    a4.然后该类就可以跟QPushbutton一样调用了,只是这个按钮比QPushbutton多了一个功能,那就是可以实现任意位置的上面图片下面文字效果,其它功能跟QPushbutton一模一样。

    MyMenuButton::MyMenuButton(QString  str_icon,QString str_text,int w,int h)
    {
           this->setFixedSize(w,h);                      //调整控件尺寸
    
           QLabel *buttonImage = new QLabel();
           buttonImage->setStyleSheet(str_icon);
    
           QLabel *buttonTxt = new QLabel();
           buttonTxt->setFixedHeight(20);
           buttonTxt->setText(str_text);
           buttonTxt->setAlignment(Qt::AlignHCenter|Qt::AlignTop);
    
           QVBoxLayout *topLayout = new QVBoxLayout();
           topLayout->addWidget(buttonImage);
           topLayout->setContentsMargins(10,10,10,0);
    
           QVBoxLayout *bottomLayout = new QVBoxLayout();
           bottomLayout->addWidget(buttonTxt);
           bottomLayout->setMargin(0);
           bottomLayout->setSpacing(0);
    
           QVBoxLayout *mainLayout = new QVBoxLayout();
           mainLayout->setMargin(0);
           mainLayout->setSpacing(5);
           mainLayout->addLayout(topLayout);
           mainLayout->addLayout(bottomLayout);
    
           this->setLayout(mainLayout);
    
    }

     --------------------- 

    作者:净无邪
    来源:CSDN
    原文:https://blog.csdn.net/naibozhuan3744/article/details/82151007
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    Sqlite EF6注册
    C# 等值锁定
    net 4.0+EF6+Sqlite 使用,安装,打包
    C#调用C++函数
    C# 调用.exe文件
    Java继承
    python多线程与threading模块
    Java对象构造
    python多线程与_thread模块
    Linux文件压缩与打包
  • 原文地址:https://www.cnblogs.com/nanqiang/p/10477680.html
Copyright © 2020-2023  润新知