2

I have to draw a custom control in QTableView. This control must looks like 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);

The code above draws QStyle::CE_PushButton - that looks like QButton, - but there is no QStyle::CE_LineEdit in Qt library. How can I draw QLineEdit?

meldo
  • 291
  • 3
  • 11

3 Answers3

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.

Tim Meyer
  • 12,210
  • 8
  • 64
  • 97
  • Control should always be visible. It’s required to solve issue described above (drawing QLineEdit as QStyle::ControlElement) for overriding drawDisplay (and drawFocus), isn’t it? But I choose another way. My delegate inherited from QStyledItemDelegate and I try to override paint and editorEvent methods. [Example for Checkbox control](http://stackoverflow.com/questions/3363190/qt-qtableview-how-to-have-a-checkbox-only-column). – meldo Oct 09 '12 at 12:02
1

I found an answer by myself. You may display a custom editor (ordinary widget) permanently using:

void QAbstractItemView::openPersistentEditor ( const QModelIndex & index )
meldo
  • 291
  • 3
  • 11
  • Can you change this topic of this question to "How to permanently display a custom widget / delegate in QTableView / QAbstractItemView?". Emphasis on the "permanently". I think this would help many people on this issue. – hansonap Oct 22 '18 at 18:42
1

First you need to understand that a button is a control Element and thus you can find it under CE but when you need a lineEdit it is not a control element. In order to paint a lineEdit, I shall quote from the qt documentation,

"QStyleOptionFrameV2 inherits QStyleOptionFrame which is used for drawing several built-in Qt widgets, including QFrame, QGroupBox, QLineEdit, and QMenu."

Yes, only a sample code that might work will help you understand it clearly! The code should somehow look like this

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);