zoukankan      html  css  js  c++  java
  • QWidget添加带有图片的QPushButton,布局QGridLayout

    QWidget* w = new QWidget(this);

    w->setGeometry(10,20,400,300);

    QVBoxLayout* layout = new QVBoxLayout(w);

    layout->setSpacing(0);

    layout->setContentMargins(0,0,0,0);

    QPushButton* b = new QPushButton(w);

    b->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    layout->addWidget(b);

    QString style = "QPushButton{image:url(:/normal.png);border-style:none;}

    style += "QPushButton:hover{image:url(:/hover.png);border-style:none;}

    style += "QPushButton:pressed{image:url(:/pressed.png);border-style:none;}

    style += "QPushButton:checked{image:url(:/checked.png);border-style:none;}

    b->setStyleSheet(style);

    要想图片随着按钮大小拉伸,可以将image换成border-image

    如果不想使用布局,自由设置按钮在widget中的位置:

    QWidget* w = new QWidget(this);

    w->setGeometry(10,20,400,300);

    QPushButton* b = new QPushButton(w);

    b->setGeometry(10,20,40,30);

    b->show();//不要忘了这个,否则看不见

    网格布局QGridLayout的使用

    QGridLayout* g = new QGridLayout(w);

    QPushButton* b1 = new QPushButton(w);b1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    QPushButton* b2 = new QPushButton(w);b2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    QPushButton* b3 = new QPushButton(w);b3->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    g->addWidget(b1,0,0,1,2);//占2行

    g->addWidget(b2,0,1,1,1);

    g->addWidget(b3,1,1,1,1);

  • 相关阅读:
    转载一个好用的div弹出层插件
    asp.net 母版页使用方法
    visual studio 代码排版组合键
    模仿米折网商品图片自动翻页效果
    BinaryWriter 、BinaryReader在读写
    Java 8 Lambda 表达式
    IBeacon协议分析
    Centos配置jdk和tomcat环境
    apidoc 生成Restful web Api文档
    数组和链表的区别
  • 原文地址:https://www.cnblogs.com/coolbear/p/11769960.html
Copyright © 2011-2022 走看看