按钮-下拉列表

点击按钮,出现一个下拉列表:

 

 主要代码:

void WgSetUp::AddItem()
{
    QIcon myicon; //新建QIcon对象
    myicon.addFile(tr("../data/skin/image/ui/MenuTools.png")); //让QIcon对象指向想要的图标
    QPushButton* SettingButton = new QPushButton(this);
    SettingButton->resize(200,60);
    SettingButton->setIcon(myicon); //给按钮添加图标
    SettingButton->setIconSize(QSize(60, 60));//设置图标大小
    SettingButton->setText("设置");
    SettingButton->setObjectName("setting");

    QFont font;
    font.setPointSize(15);
    SettingButton->setFont(font);
    connect(SettingButton,&QPushButton::clicked,this,&WgSetUp::SlotSettingClicked);
}
void WgSetUp::SlotSettingClicked()
{
    QObject* obj = sender();
    QPushButton* pBtn = dynamic_cast<QPushButton*>(obj);

    if(m_listSettingChoose != nullptr)
    {
        if(m_listSettingChoose->isHidden())
        {
            m_listSettingChoose->show();
        }else
        {
            m_listSettingChoose->hide();
        }
        return;
    }

    m_listSettingChoose = new QListWidget(this);
    m_listSettingChoose->setGeometry(pBtn->pos().x(),
                      pBtn->pos().y()+pBtn->height(),
                      pBtn->width(),300);
    m_listSettingChoose->show();
    m_listSettingChoose->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QListWidgetItem* item0 = new QListWidgetItem(QIcon("../data/skin/image/ui/0.png"),"1111");
    QListWidgetItem* item1 = new QListWidgetItem(QIcon("../data/skin/image/ui/1.png"),"2222");
    QListWidgetItem* item2 = new QListWidgetItem(QIcon("../data/skin/image/ui/2.png"),"3333");
    QListWidgetItem* item3 = new QListWidgetItem(QIcon("../data/skin/image/ui/MenuExportPic.png"),"导出图片");
    QListWidgetItem* item4 = new QListWidgetItem(QIcon("../data/skin/image/ui/MenuMakeVideo.png"),"制作视频");
    m_listSettingChoose->addItem(item0);
    m_listSettingChoose->addItem(item1);
    m_listSettingChoose->addItem(item2);
    m_listSettingChoose->addItem(item3);
    m_listSettingChoose->addItem(item4);

    m_listSettingChoose->setStyleSheet("QListWidget{border:1px solid blue;   /*边界线:宽度、颜色*/"
                        "background:rgba(0, 0, 0, 0.2);"  /*背景色*/
                         "color:white; "         /*前景色:文字颜色*/
                         "font: 15 9pt;"
                        " margin:10px,10px,0px,50px;/*上、下、左、右,间距*/}"
                        "QListWidget::Item{padding-left:2px;height: 55px;}"
                        "QListWidget::Item:hover{background:transparent; }"
                        "/*QListWidget::item:selected{background:transparent;*/}");

    connect(m_listSettingChoose,&QListWidget::pressed,[=](QModelIndex pos)
    {
        switch(pos.row()){
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            //导出图片
            Screenshots();
            break;
        case 4:
            //制作视频
            RecordVideo();
            break;

        default:
            break;
        }
    });
}

 

posted @ 2022-12-05 14:37  阳光下的小土豆  阅读(41)  评论(0编辑  收藏  举报