-3

I created a tablewidget like this:

Tablewidget

I want to edit cell(0) value, (double click), but the edit box was too big and it covers cell(1):

Tablewidget2

How do I avoid the edit box covering the cell after it?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Stone
  • 1
  • 2
  • call `table->resizeColumnsToContents();` [resizeColumnsToContents](http://doc.qt.io/qt-4.8/qtableview.html#resizeColumnsToContents) – Simon Nov 21 '17 at 10:01
  • I add table->resizeColumnsToContents(); but the editbox still too big to cover the cell after it。 – Stone Nov 22 '17 at 01:38

1 Answers1

0

You should make your own children QStyledItemDelegate and redefine QStyledItemDelegate::createEditor method.

Something like that:

QWidget * MyStyledItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const 
{
    QWidget * editor = QStyledItemDelegate::createEditor(parent, option, index);
    editor->setWidth( 20 ); // Handle editor here.
    return editor;
}
stanislav888
  • 374
  • 2
  • 9
  • QTableWidget *table;table->setItemDelegate(new MyStyledItemDelegate); it really worked !Thanks a lot! – Stone Nov 23 '17 at 05:33
  • another question,if I want setWidth(width);I must pass a width param to MyStyleItemDelegate . How to do it ? Thanks. – Stone Nov 29 '17 at 13:32
  • That common C++ issue. How to pass parameter on your class. Many and many ways, 1) Via constructor of MyStyledItemDelegate() *table;table->setItemDelegate(new MyStyledItemDelegate(20)); You need redefine constructor. 2) Via "setter" function MyStyledItemDelegate * myDelegate = new MyStyledItemDelegate; myDelegate->setEditorWidth(20); Need implement setEditorWidth() – stanislav888 Dec 01 '17 at 12:31