Solved Changes in table with combo boxes
-
Thanks for your code.
I have played, probably better to say messed around, a bit and seem to semi-understand what you are doing there.
For my trial I have basically used the wiki example and added your ComboBoxDelegate.The wiki example starts out with an empty 4 by 10 table.
When simply adding a line
results->setEditable(true);
in createEditor I got something to work.
The only problem was the linem_comboProxy->sort(0);
The behaviour of the QComboBox was not as expected. It basically showed only a blank line and last value entered. When choosing a different column, it showed the performance as expected, but that should be wrong, since the number of columns stays at 1. I guess it has to do with the incomplete use of the whole model.
The moment I will bypass the QSortFilterProxyModel and use directly the instance of the QStandardItemModel instead.
When I am using your source mostly as is, it provides an increasing list of entries of the whole table already done. Since I need the same basically for column-wise different entries, the solution will be a couple of instances of QStandardItemModel.
Can you offer an explanation why the sort does not work?
-
@koahnig said in Changes in table with combo boxes:
Since I need the same basically for column-wise different entries, the solution will be a couple of instances of QStandardItemModel.
No, just call
setItemDelegateForColumn
in the view with a different instance of the delegate for every column. This way is more reusable and scalable@koahnig said in Changes in table with combo boxes:
When choosing a different column
strange, [this] should take care of the resorting. Otherwise use
QComboBox::InsertAlphabetically
sort policy on the combobox directly -
@VRonin said in Changes in table with combo boxes:
@koahnig said in Changes in table with combo boxes:
Since I need the same basically for column-wise different entries, the solution will be a couple of instances of QStandardItemModel.
No, just call
setItemDelegateForColumn
in the view with a different instance of the delegate for every column. This way is more reusable and scalableYep, found already that I was thinking too complecated ;)
@VRonin said in Changes in table with combo boxes:
@koahnig said in Changes in table with combo boxes:
When choosing a different column
strange, [this] should take care of the resorting. Otherwise use
QComboBox::InsertAlphabetically
sort policy on the combobox directlyInsertAlphabetically does the trick.
However, I still have to bypass the proxymodel as in here:
//#define USE_COMBO_PROXY QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const { QComboBox* result = new QComboBox(parent); result->setEditable( true ); result->setInsertPolicy( QComboBox::InsertAlphabetically ); #ifdef USE_COMBO_PROXY m_comboProxy->sort( 0 ); result->setModel(m_comboProxy); #else result->setModel(m_comboModel); #endif return result; }
-
Currently I am testing with this class derived from QTablewidget
header#ifndef MYTABLEWIDGET_H #define MYTABLEWIDGET_H #include <QTableWidget> class MyTableWidget : public QTableWidget { public: MyTableWidget(); void setItem ( int row, int col, const QString & text ); }; #endif // MYTABLEWIDGET_H
Source
#include "MyTableWidget.h" #include "ComboBoxDelegate.h" MyTableWidget::MyTableWidget() { ComboBoxDelegate *vr = new ComboBoxDelegate ( this ); setColumnCount(4); setItemDelegateForColumn ( 1, vr ); setItemDelegateForColumn ( 2, new ComboBoxDelegate ( this ) ); setItemDelegateForColumn ( 3, new ComboBoxDelegate ( this ) ); setRowCount(10); resize(600,400); setItem ( 1, 0, "test 1 0" ); setItem ( 1, 1, "test 1 1" ); setItem ( 1, 2, "test 1 2" ); } void MyTableWidget::setItem ( int row, int col, const QString & text ) { QTableWidget::setItem ( row, col, new QTableWidgetItem ( text ) ); ComboBoxDelegate *vr = qobject_cast < ComboBoxDelegate * > ( this->itemDelegateForColumn ( col ) ); if ( vr ) { vr->addItem ( text, row ); } }
And a main
#include <QApplication> #include "MyTableWidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MyTableWidget tw; tw.show(); return a.exec(); }
-
You probably need to tweak the
setEditorData
method.qobject_cast<QComboBox*>(editor)->setCurrentText(index.model()->data(index, Qt::EditRole).toString());
assumes theindex.data(Qt::EditRole).toString()
is already in the combo box list which might not be the case. change it to:{ auto comboEditor =qobject_cast<QComboBox*>(editor); Q_ASSERT(comboEditor); const QString& textToFind = index.data(Qt::EditRole).toString(); int itemIdx = comboEditor->findText(textToFind); if(itemIdx<0){ comboEditor->addItem(textToFind); itemIdx = comboEditor->findText(textToFind); } Q_ASSERT(itemIdx>=0); comboEditor->setCurrentIndex(itemIdx); }
EDIT fixed compile fail and wrong Q_ASSERT condition
-
Thanks for update.
Unfortunately, the tweaking does not work.
setEditorData
is a const method. Therefore, the compiler complains aboutaddItem
use.Just for completeness. The main tests on my side are on Win 10 and Qt 5.4.2 with MinGW. I have checked it also with Qt 5.9.1 for MinGW. The use of proxy model is always an issue. However, the behaviour is different for both versions.
The major issue is for Qt 5.4.2 that ComboBox entries held are 2 text plus a varying number blank lines at top.
For Qt 5.9.1 it is the opposite. Apparently, two blank lines at the end and varying number of entries at top, but also some are missing.
I am giving up for testing at the time, since my knowledge is not sound enough, yet. Also is the functionality with by-passing of proxy model sufficient for my current use case.However, it is time give the following statements.
Even though I am "complaining" about some deficiencies in your code, I see it more as a feedback in case you need some input.
Your assistance helped significantly improving my speed in getting a workable solution.A huge thanks for turbo-teaching!!
-
@koahnig said in Changes in table with combo boxes:
the compiler complains about addItem use.
you can still copy the entire content of additem in that block or exploit the combo and call
comboEditor->addItem
instead. also, probably it's worth preventing empty (or even whitespace only) strings to be addedThe blank lines are weird. you could check what is going on by inquiring
m_comboProxy->rowCount()
-
Code of
setEditorData
{ auto comboEditor =qobject_cast<QComboBox*>(editor); Q_ASSERT(comboEditor); const QString& textToFind = index.data(Qt::EditRole).toString(); int itemIdx = comboEditor->findText(textToFind); if(itemIdx<0){ comboEditor->addItem(textToFind); itemIdx = comboEditor->findText(textToFind); qDebug() << "addItem " << textToFind << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount(); } Q_ASSERT ( itemIdx >= 0 ); comboEditor->setCurrentIndex(itemIdx); qDebug() << "in combo box " << comboEditor->currentText() << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount();; }
Double click on 4, 2
Typing "Testing" <enter>
Double click on 6, 2
Typing "erf"
Double click 8, 2
Typing "Zufall"output window:
Starting r:\build-AnotherComboBoxTest-Desktop_Qt_5_9_1_MinGW_32bit-Debug\debug\AnotherComboBoxTest.exe... addItem "" 0 2 1 in combo box "" 0 2 1 in combo box "testing" 2 3 1 in combo box "testing" 2 3 1 in combo box "" 0 3 1 in combo box "erf" 1 4 1 in combo box "erf" 1 4 1 in combo box "" 0 4 1 in combo box "" 0 5 1 in combo box "" 0 5 1
That's the outcome
-
Yep, that's what I meant with:
@VRonin said in Changes in table with combo boxes:
also, probably it's worth preventing empty (or even whitespace only) strings
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { if (index.data(Qt::EditRole).isNull()) qobject_cast<QComboBox*>(editor)->setCurrentIndex(-1); else { auto comboEditor =qobject_cast<QComboBox*>(editor); Q_ASSERT(comboEditor); const QString& textToFind = index.data(Qt::EditRole).toString(); //make sure you are not just searching whitespace const QRegularExpression realTextRegExp("\S+"); if(!realTextRegExp.match(textToFind).hasMatch()){ comboEditor->setCurrentIndex(-1); return; } int itemIdx = comboEditor->findText(textToFind); if(itemIdx<0){ comboEditor->addItem(textToFind); itemIdx = comboEditor->findText(textToFind); qDebug() << "addItem " << textToFind << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount(); } Q_ASSERT ( itemIdx >= 0 ); comboEditor->setCurrentIndex(itemIdx); qDebug() << "in combo box " << comboEditor->currentText() << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount(); } }
-
I am sorry to correct, but it doesn't help either.
The problem is with the index coming into
setEditorData
Here is my latest code:
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { if (index.data(Qt::EditRole).isNull()) qobject_cast<QComboBox*>(editor)->setCurrentIndex(-1); else { auto comboEditor =qobject_cast<QComboBox*>(editor); Q_ASSERT(comboEditor); qDebug() << " index " << index; const QString& textToFind = index.data(Qt::EditRole).toString(); qDebug() << "textToFind before reg exp " << textToFind; //make sure you are not just searching whitespace const QRegularExpression realTextRegExp("\\S+"); if( ! realTextRegExp.match(textToFind).hasMatch()){ qDebug() << "textToFind " << textToFind; comboEditor->setCurrentIndex(-1); return; } int itemIdx = comboEditor->findText(textToFind); if(itemIdx<0){ comboEditor->addItem(textToFind); itemIdx = comboEditor->findText(textToFind); qDebug() << "addItem " << textToFind << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount(); } Q_ASSERT ( itemIdx >= 0 ); comboEditor->setCurrentIndex(itemIdx); qDebug() << "in combo box " << comboEditor->currentText() << " " << itemIdx << " " << m_comboProxy->rowCount() << " " << m_comboProxy->columnCount(); } }
That is the latest output:
Starting r:\build-AnotherComboBoxTest-Desktop_Qt_5_9_1_MinGW_32bit-Debug\debug\AnotherComboBoxTest.exe... no delegate found addItem "test 1 1" E:\Source\AnotherComboBoxTest\MyTableWidget.cpp 28 addItem "test 1 2" E:\Source\AnotherComboBoxTest\MyTableWidget.cpp 28 index QModelIndex(3,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "testing" in combo box "testing" 1 2 1 index QModelIndex(3,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "testing" in combo box "testing" 1 2 1 index QModelIndex(5,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "erf" in combo box "erf" 0 3 1 index QModelIndex(5,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "erf" in combo box "erf" 0 3 1 index QModelIndex(7,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "" textToFind "" index QModelIndex(7,1,0x0,QTableModel(0x175fb6a8)) textToFind before reg exp "" textToFind ""
In additiion the update problems there are also the initial input for this test "test 1 1" not covered anymore after some input directly to the table.
-
Isn't that the correct output? what were you expecting?
-
Nope, I did the same as with the previous tests. Only lower case letters this time. The input was:
- Double click on 4, 2
- Typing "testing" <enter>
- Double click on 6, 2
- Typing "erf" <enter>
- Double click 8, 2
- Typing "zufall" <enter>
-
We are back to the start :(
#define USE_COMBO_PROXY QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const { QComboBox* result = new QComboBox(parent); result->setEditable( true ); result->setInsertPolicy( QComboBox::InsertAlphabetically ); #ifdef USE_COMBO_PROXY // m_comboProxy->sort( 0 ); result->setModel(m_comboProxy); #else result->setModel(m_comboModel); #endif return result; }
Commenting out
m_comboProxy->sort( 0 )
or by-passing the whole proxy model do work correctly. -
Sorry but now I'm lost. could you describe what the expected output was?
-
for anyone ending in this post from google, you should add
m_comboProxy->setDynamicSortFilter(false);
in the constructor of the delegate -
Yes, this does the trick.
After introducing the line in the constructor the test example is working.Once again, many thanks for your help.