QT编程之简单的按钮下拉菜单

效果图如下


不太喜欢说太多,核心代码贴上

QPushButton *statusputton;
QMenu *statusmenu;
void Dialog::creatpushbotton()
{
statusputton=new QPushButton();
statusmenu =new QMenu();
QAction *online=new QAction(statusmenu);
QAction *chatme=new QAction(statusmenu);
QAction *busy=new QAction(statusmenu);
QAction *leave=new QAction(statusmenu);
QAction *steath=new QAction(statusmenu);
QAction *disturb=new QAction(statusmenu);

//设置
online->setIcon(icon:/img/online.png);
chatme->setIcon(icon:/img/chatme.png);
busy->setIcon(icon:/img/busy.png);
leave->setIcon(icon:/img/leave.png);
steath->setIcon(icon:/img/steath.png);
disturb->setIcon(icon:/img/disturb.png);

//
statusmenu->addAction(online);
statusmenu->addAction(chatme);
statusmenu->addAction(busy);
statusmenu->addAction(leave);
statusmenu->addAction(steath);
statusmenu->addAction(disturb);

statusputton->setMenu(statusmenu);

online->setText(“在线”);
chatme->setText(“Q我吧”);
busy->setText(“忙碌”);
leave->setText(“离开”);
steath->setText(“隐身”);
disturb->setText(“请勿打扰”);

statusputton->setStyleeSheet("QPushButton::menu-indicator{image:None;}");
connecr(statusmenu,SIGNAL(),this,SLOT(setstatus(QAction *)));
}

void Dialog::setstatus(QAction *action)
{
statusputton->setIcon(action->icon());

}






你可能感兴趣的:(QT编程之简单的按钮下拉菜单)