I attach a new file (k2.cpp, see also listing). It demonstrates a problem: when you press the combo, it writes the current index of the view() of the combo box. It is the first entry for the first time (which is incorrect, because the current index is -1). If you move the mouse to the second line, then close the popup, and click to open popup again, the current index is now correctly -1.
#include <QtGui>
#include <QModelIndex>
#include <iostream>
using namespace std;
public:
void addItem
(const char* s
){QComboBox::addItem(s
);
}
void setCurrentIndex
(int i
){QComboBox::setCurrentIndex(i
);
}
void showPopup(){
cout<<"mi.isValid()=="<<mi.isValid()<<", mi.row()=="<<mi.row()<<", mi.column()=="<<mi.column()<<endl;
}
};
int main(int argc, char* argv[])
{
dialog.resize(300,200);
ComboBox* combo=new ComboBox(&dialog);
combo->addItem("first");
combo->addItem("second");
combo->addItem("third");
combo->addItem("fourth");
combo->setCurrentIndex(-1);
dialog.show();
return app.exec();
return 0;
}
#include <QtGui>
#include <QModelIndex>
#include <iostream>
using namespace std;
class ComboBox:public QComboBox{
public:
ComboBox(QWidget* parent):QComboBox(parent){}
void addItem(const char* s){QComboBox::addItem(s);}
void setCurrentIndex(int i){QComboBox::setCurrentIndex(i);}
void showPopup(){
QComboBox::showPopup();
QModelIndex mi=view()->currentIndex();
cout<<"mi.isValid()=="<<mi.isValid()<<", mi.row()=="<<mi.row()<<", mi.column()=="<<mi.column()<<endl;
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QDialog dialog;
dialog.resize(300,200);
ComboBox* combo=new ComboBox(&dialog);
combo->addItem("first");
combo->addItem("second");
combo->addItem("third");
combo->addItem("fourth");
combo->setCurrentIndex(-1);
dialog.show();
return app.exec();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks