PDA

View Full Version : QComboBox minor problem



lalesculiviu
11th December 2009, 17:31
This is a problem for Qt 4.6.0. For 4.5.3 I think it did not exist.

I have a minor problem with QComboBox: I insert items, and make current index -1, so that no item is selected. But when I press the down arrow and go with the mouse over the first entry, it is not highlighted. Only if I go to the second entry and then back to the first entry, the first entry is highlighted.

I attach a small code:



#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QDialog dialog;

dialog.resize(300,200);

QComboBox* combo=new QComboBox(&dialog);

combo->addItem("first");
combo->addItem("second");
combo->addItem("third");
combo->addItem("fourth");

combo->setCurrentIndex(-1);

dialog.show();

return app.exec();

return 0;
}

Lykurg
12th December 2009, 10:51
Works fine here.

lalesculiviu
12th December 2009, 16:50
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;

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