我的Qt对话框上有几个组合框和双旋转盒。现在,我需要一个菜单上的“ResetToDefault”项目,当你右键单击小部件(旋转框或组合框)时会出现该项目。
我怎么得到它。是否有什么方法,我可以有一个自定义菜单出现在右击,或有一种方式,我可以添加项目的菜单,来右击。
发布于 2018-08-02 17:36:06
对于Qt 4,可以通过使用自己的QLineEdit对可编辑的QComboBox执行此操作。创建一个派生的QLineEdit类,实现contextMenuEvent
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget* parent = 0) : QLineEdit(parent){}
void contextMenuEvent(QContextMenuEvent *event)
{
QPointer<QMenu> menu = createStandardContextMenu();
//add your actions here
menu->exec(event->globalPos());
delete menu;
}
};
然后,使用QComboBox的setLineEdit函数设置行编辑
MyLineEdit* edit = new MyLineEdit();
comboBox->setLineEdit(edit);
https://stackoverflow.com/questions/-100000489
复制相似问题