我试图以一种用户可以从下拉列表中删除项目的方式调整QComboBox的ui(不先选择它们)。
背景是我正在使用QComboBox来指示现在打开哪个数据文件。我也将它用作最近打开的文件的缓存。我希望用户能够删除他不想再列出的条目。这可以是通过点击删除键,或上下文菜单,或任何直接实现的内容。我不想依赖于首先选择项目。在Firefox中可以找到类似的行为,其中可以删除对条目字段的旧缓存建议。
我正在考虑对QComboBox使用的列表视图进行子类化,但是,我没有找到足够的文档来帮助我开始。
如果有任何提示和建议,我将不胜感激。我正在使用PyQt,但对C ++示例没有任何问题。
答案 0 :(得分:5)
我使用installEventFilter文档中的代码解决了这个问题。
//must be in a header, otherwise moc gets confused with missing vtable
class DeleteHighlightedItemWhenShiftDelPressedEventFilter : public QObject
{
Q_OBJECT
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
bool DeleteHighlightedItemWhenShiftDelPressedEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key::Key_Delete && keyEvent->modifiers() == Qt::ShiftModifier)
{
auto combobox = dynamic_cast<QComboBox *>(obj);
if (combobox){
combobox->removeItem(combobox->currentIndex());
return true;
}
}
}
// standard event processing
return QObject::eventFilter(obj, event);
}
myQComboBox->installEventFilter(new DeleteHighlightedItemWhenShiftDelPressedEventFilter);
答案 1 :(得分:4)
comboBox->removeItem(int index) // removes item at index
答案 2 :(得分:0)
您可以创建一个专门的类来自动执行某些过程,从而最终节省时间。例如,在https://phabricator.kde.org/D15693(对于名为Krusader的程序中)中,可以看到一个名为KrHistorComboBox
(继承自KHistoryComboBox)的新类,并且可以使用它。但这一次,对于这个答案:这是一个直接继承自QComboBox
的版本,以及一个使用示例:
文件main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
文件mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
文件mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "krhistorcombobox.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Creates a new editable comboBox, and populates it with data
KrHistorComboBox *combox;
combox = new KrHistorComboBox(this);
combox->setEditable(true);
QStringList elementsToAdd = {"one", "two", "three"};
combox->insertItems(0, elementsToAdd);
}
MainWindow::~MainWindow()
{
delete ui;
}
文件krhistorcombobox.h
/*****************************************************************************
* Copyright (C) 2018 Shie Erlich <krusader@users.sourceforge.net> *
* Copyright (C) 2018 Rafi Yanai <krusader@users.sourceforge.net> *
* Copyright (C) 2018 Krusader Krew [https://krusader.org] *
* *
* This file is part of Krusader [https://krusader.org]. *
* *
* Krusader is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* Krusader is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Krusader. If not, see [http://www.gnu.org/licenses/]. *
*****************************************************************************/
#ifndef KRHISTORCOMBOBOX_H
#define KRHISTORCOMBOBOX_H
// QtWidgets
#include <QComboBox>
/*! A KrHistorComboBox event filter that e.g. deletes the current item when the user presses Shift+Del
There was more information in https://doc.qt.io/qt-5/qobject.html#installEventFilter,
https://forum.qt.io/post/160618 and https://stackoverflow.com/questions/17820947/remove-items-from-qcombobox-from-ui/52459337#52459337
*/
class KHBoxEventFilter : public QObject
{
Q_OBJECT
public:
explicit KHBoxEventFilter(QObject *parent = nullptr) : QObject(parent) {}
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
};
//! An event filter for the popup list of a KrHistorComboBox, e.g. it deletes the current item when the user presses Shift+Del
class KHBoxListEventFilter : public QObject
{
Q_OBJECT
public:
explicit KHBoxListEventFilter(QObject *parent = nullptr) : QObject(parent) {}
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
};
//! A specialized version of a QComboBox, e.g. it deletes the current item when the user presses Shift+Del
class KrHistorComboBox : public QComboBox
{
Q_OBJECT
public:
explicit KrHistorComboBox(QWidget *parent = nullptr);
protected:
KHBoxEventFilter boxEF;
KHBoxListEventFilter listEF;
};
#endif // KRHISTORCOMBOBOX_H
文件krhistorcombobox.cpp
/*****************************************************************************
* Copyright (C) 2018 Shie Erlich <krusader@users.sourceforge.net> *
* Copyright (C) 2018 Rafi Yanai <krusader@users.sourceforge.net> *
* Copyright (C) 2018 Krusader Krew [https://krusader.org] *
* *
* This file is part of Krusader [https://krusader.org]. *
* *
* Krusader is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* Krusader is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Krusader. If not, see [http://www.gnu.org/licenses/]. *
*****************************************************************************/
#include "krhistorcombobox.h"
// QtCore
#include <QEvent>
// QtGui
#include <QKeyEvent>
// QtWidgets
#include <QAbstractItemView>
bool KHBoxEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
auto keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->modifiers() == Qt::ShiftModifier && keyEvent->key() == Qt::Key::Key_Delete) {
auto box = dynamic_cast<QComboBox *>(obj);
if (box != nullptr) {
// Delete the current item
box->removeItem(box->currentIndex());
return true;
}
}
}
// Perform the usual event processing
return QObject::eventFilter(obj, event);
}
bool KHBoxListEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
auto keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->modifiers() == Qt::ShiftModifier && keyEvent->key() == Qt::Key::Key_Delete) {
auto iv = dynamic_cast<QAbstractItemView *>(obj);
if (iv->model() != nullptr) {
// Delete the current item from the popup list
iv->model()->removeRow(iv->currentIndex().row());
return true;
}
}
}
// Perform the usual event processing
return QObject::eventFilter(obj, event);
}
KrHistorComboBox::KrHistorComboBox(QWidget *parent) : QComboBox(parent)
{
installEventFilter(&boxEF);
QAbstractItemView* iv = view();
if (iv != nullptr)
iv->installEventFilter(&listEF);
}
文件krhcexample.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-09-22T18:33:23
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
krhistorcombobox.cpp \
mainwindow.cpp
HEADERS += \
krhistorcombobox.h \
mainwindow.h
FORMS += \
mainwindow.ui
文件mainwindow.ui
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
这是正在执行的示例的屏幕截图: The program before pressing Shift+Del, which would remove the option named "two"
注意:当前答案中的某些源代码基于https://doc.qt.io/qt-5/qobject.html#installEventFilter,https://forum.qt.io/post/160618和https://stackoverflow.com/a/26976984中名为“ nwp”的用户的良好工作(尽管该答案不包括如果弹出窗口列表中的元素正在被查看,并且有一个“内存泄漏”(一个对象被构造但未销毁),则删除该列表中的元素的代码,因此,如果开发人员添加了析构函数,例如~DeleteHighlightedItemWhenShiftDelPressedEventFilter() { QTextStream(stdout) << "DESTRUCTED" << endl; }
后来发现析构函数的代码从未执行过,因此存在内存泄漏;目前我没有在https://stackoverflow.com/a/26976984中添加注释的stackoverflow点。
答案 3 :(得分:0)
很抱歉,对于该线程的介绍太晚了,但是我想贡献我发现的其他方法,以防万一有人在找我。该方法已通过Qt 5.6进行了测试。我不能保证它们可以在其他版本中使用。
一种可能性是听QCombobox的view()的“ pressed()”信号。这样,我们可以使用鼠标右键从列表中删除项目。我很惊讶地看到view()始终可用,从不为NULL,并且可以在显示项目时将其删除,因此以下各项工作得很好:
class MyCombobox : public QComboBox
{
Q_OBJECT
public: MyCombobox(QWidget *pParent = NULL);
protected slots: void itemMouseDown(const QModelIndex &pIndex);
};
MyCombobox::MyCombobox(QWidget *pParent)
{
connect( QComboBox::view(), SIGNAL(pressed(const QModelIndex &)),
this, SLOT(itemMouseDown(const QModelIndex &)) );
}
void MyCombobox::itemMouseDown(const QModelIndex &pIndex)
{
if( QApplication::mouseButtons() == Qt::RightButton )
{
QComboBox::model()->removeRow(pIndex.row());
}
}
第二个选项是在事件视图中安装事件过滤器。这样,我们可以使用Delete键或其他任何方式删除项目。测试NULL指针和无效的行索引可能是一个好主意,但是为了清楚起见,我省略了它。
class MyCombobox : public QComboBox
{
Q_OBJECT
public: MyCombobox(QWidget *pParent = NULL);
protected: bool eventFilter(QObject *pWatched, QEvent *pEvent);
};
MyCombobox::MyCombobox(QWidget *pParent)
{
QComboBox::view()->installEventFilter(this);
}
bool MyCombobox::eventFilter(QObject *pWatched, QEvent *pEvent)
{
if( pEvent->type() == QEvent::KeyPress )
{
QKeyEvent *tKeyEvent = static_cast<QKeyEvent*>(pEvent);
if( tKeyEvent->key() == Qt::Key_Delete )
{
QComboBox::model()->removeRow(QComboBox::view()->currentIndex().row());
return true;
}
}
return QObject::eventFilter(pWatched, pEvent);
}
就是这样。