2

我必须在 QTableView 中绘制一个自定义控件。此控件必须看起来像 FileChooser。

FileChooser http://www.vision.ee.ethz.ch/computing/sepp-irix/qt-3.0-mo/filechooser.png

QStyleOptionButton button_option;
button_option.state |= QStyle::State_Enabled | QStyle::State_Off;
button_option.rect = PushButtonRect(option); //calculate button rect
button_option.text = "...";
QApplication::style()->drawControl(
    QStyle::CE_PushButton,
    &button_option,
    painter);

上面的代码绘制了 QStyle::CE_PushButton - 看起来像 QButton,但 Qt 库中没有 QStyle::CE_LineEdit。如何绘制 QLineEdit?

4

3 回答 3

1

In order to draw custom widgets in a Table View, you need to create a custom QItemDelegate subclass and override at least the createEditor method, where you can create any kind of widget which is displayed when double-clicking into the table cell. This item delegate can be assigned to the respective column in your table view.

You would then need to create a separate class e.g. CustomFileChooser which inherits from QWidget and consists of a Line Edit and Button.

Your createEditor method would then return such an object.

You may also have to override setEditorData (which shall assign the current model value to the editor widget which was created) and setModelData (which is called when the changes are committed).

This way, the line edit and button would only be visible after double-clicking into the table cell. If you want it to be always visible, you will have to override drawDisplay() as well.

于 2012-10-09T11:21:01.197 回答
1

我自己找到了答案。您可以使用以下方式永久显示自定义编辑器(普通小部件):

void QAbstractItemView::openPersistentEditor ( const QModelIndex & index )
于 2012-10-11T12:11:43.310 回答
1

首先,您需要了解按钮是控制元素,因此您可以在 CE 下找到它,但是当您需要 lineEdit 时,它不是控制元素。为了画线编辑,我将引用 qt 文档,

“QStyleOptionFrameV2 继承了 QStyleOptionFrame,它用于绘制几个内置的 Qt 小部件,包括 QFrame、QGroupBox、QLineEdit 和 QMenu。”

是的,只有一个可能有效的示例代码才能帮助您清楚地理解它!代码应该看起来像这样

QStyleOptionFrameV2 *panelFrame = new QStyleOptionFrameV2;
QLineEdit *search = new QLineEdit;
panelFrame->initFrom(search);
panelFrame->rect = QRect(x,y,w,h);//Indeed the location and the size
panelFrame->lineWidth = QApplication::style->pixelMetric(QStyle::PM_DefaultFrameWidth, panelFrame, search);
panelFrame->state |= QStyle::State_Sunken;
QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, panelFrame, painter);
于 2016-05-23T09:36:49.880 回答