Problems with QComboBox first time



  • We have an QItemDelgate. In the fisrt column I have a comboBox. I want that with one click show popup but the first time the popup dissapear. Do you any solution for this?

    ui->tableViewTma->setItemDelegate(
    			new LrgQComboBoxCapReductionsItemDelegate(reasonList));
    ui->tableViewTma->setModel(tmaCapRedModel);
    
    
    LrgQComboBoxCapReductionsItemDelegate::LrgQComboBoxCapReductionsItemDelegate(QStringList reasonList,QObject *parent) :
    	QItemDelegate(parent)
    {
    	observable = LrbOptimalScheduleObservable::GetInstance();
    	observable->AddObserver(this);
    
    	reason = reasonList;
    }
    
    QWidget* LrgQComboBoxCapReductionsItemDelegate::createEditor(
    		QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    	if(index.column() == 1)
    	{
    		QComboBox* comboBoxTimesIni = new QComboBox(parent);
    		comboBoxTimesIni->installEventFilter(const_cast<LrgQComboBoxCapReductionsItemDelegate*>(this));
    		comboBoxTimesIni->addItems(timesIni);
    		return comboBoxTimesIni;
    	}
    	else if (index.column() == 2)
    	{
    		QComboBox* comboBoxTimesFinal = new QComboBox(parent);
    
    		QString value = index.model()->data(index.model()->index(index.row(),1), Qt::DisplayRole).toString();
    		QStringList timesFinal;
    		int indexValues = timesIni.indexOf(value);
    		if(indexValues == -1)
    		{
    			indexValues = 0;
    		}
    		for (int i = indexValues; i < observable->getOptimalData()->getRunwaySystemMessasge().size(); i++)
    		{
    			Hmi::LriRunwaySystem rs = observable->getOptimalData()->getRunwaySystemMessasgeByIndex(i);
    			timesFinal.push_back(QString::fromStdString(rs.getFinalTime()));
    		}
    		comboBoxTimesFinal->addItems(timesFinal);
    		return comboBoxTimesFinal;
    	}
    	else if ((index.column() == 3) ||
    			 (index.column() == 4) ||
    			 (index.column() == 5))
    	{
    		QLineEdit* lineEdit = new QLineEdit(parent);
    		lineEdit->setValidator( new QIntValidator(1, 100, parent) );
    		return lineEdit;
    	}
    	else if(index.column() == 6)
    	{
    		QComboBox* comboBoxReason = new QComboBox(parent);
    		comboBoxReason->addItems(reason);
    		return comboBoxReason;
    	}
    	else{
    		return QItemDelegate::createEditor(parent, option, index);
    	}
    
    }
    void LrgQComboBoxCapReductionsItemDelegate::setEditorData(
    		QWidget *editor, const QModelIndex &index) const
    {
    	if((index.column() == 1) || (index.column() == 2) || (index.column() == 6))
    	{
    		QString value = index.model()->data(index, Qt::EditRole).toString();
    		QComboBox* comboBox = static_cast<QComboBox*>(editor);
    		comboBox->setCurrentIndex(comboBox->findText(value));
    	}
    	else if ((index.column() == 3) || (index.column() == 4) || (index.column() == 5))
    	{
    		QString value = index.model()->data(index, Qt::EditRole).toString();
    		QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
    		lineEdit->setText(value);
    	}
    	else
    	{
    		QItemDelegate::setEditorData(editor, index);
    	}
    }
    void LrgQComboBoxCapReductionsItemDelegate::setModelData(
    		QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
    	if((index.column() == 1) || (index.column() == 2) || (index.column() == 6))
    	{
    		QComboBox* comboBox = static_cast<QComboBox*>(editor);
    		QString value = comboBox->currentText();
    		model->setData(index, value, Qt::EditRole);
    		//ISE#615
    		if(index.column() == 1)
    		{
    			QModelIndex indexAux = index.model()->index(index.row(), 2);
    			QString value = "";
    			model->setData(indexAux, value, Qt::EditRole);
    		}
    	}
    	else if ((index.column() == 3) || (index.column() == 4) || (index.column() == 5))
    	{
    		QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
    		QString value = lineEdit->text();
    		model->setData(index, value, Qt::EditRole);
    	}
    	else
    	{
    		QItemDelegate::setModelData(editor, model, index);
    	}
    }
    
    bool LrgQComboBoxCapReductionsItemDelegate::eventFilter(QObject* src, QEvent* evt)
    {
    	if (evt->type() == QEvent::FocusIn)
    	{
    		QComboBox* cb = qobject_cast<QComboBox*> (src);
    		if (cb)
    		{
    			cb->showPopup();
    //important to handle it only the first time, otherwise will result in
    //focus glitches
    			cb->removeEventFilter(this);
    		}
    	}
    	return QItemDelegate::eventFilter(src, evt);
    }
    
    void LrgQComboBoxCapReductionsItemDelegate::Notify()
    {
    	//ISE#385
    	timesIni.clear();
    	//timesFinal.clear();
    	//END ISE#385
    	int intervals =
    			((unsigned)observable->getOptimalData()->getRunwaySystemMessasge().size()
    				< ConstantsRmanHmi::numberOfIntervals) ?
    						observable->getOptimalData()->getRunwaySystemMessasge().size() :
    				ConstantsRmanHmi::numberOfIntervals;
    	for (int i = 0; i < intervals; i++)
    	{
    		Hmi::LriRunwaySystem rs = observable->getOptimalData()->getRunwaySystemMessasgeByIndex(i);
    		timesIni << QString::fromStdString(rs.getIniTime());
    	}
    }
    
    class LrmOneClickTableView: public QTableView
    {
    public:
    	LrmOneClickTableView(QWidget *parent = 0)
    			: QTableView(parent)
    	{
    	   setEditTriggers(QAbstractItemView::EditKeyPressed | QAbstractItemView::SelectedClicked);
    	}
    
    protected:
    	void mousePressEvent(QMouseEvent *event)
    	{
    		if (event->button() == Qt::LeftButton)
    		{
    			QModelIndex index = indexAt(event->pos());
    			if ((index.column() == 1) || (index.column() == 2) || (index.column() == 6))
    			{ // column you want to use for one click
    				setCurrentIndex(index);
    				edit(index);
    			}
    		}
    		QTableView::mousePressEvent(event);
    	}
    };


  • Hi,

    You can try calling showPopup() of the combobox where you create the instance like withing createEditor() function.

    Regards,
    Sam



  • @Sam If a put showPopup in the createEditor Function I have the same problem. It is only in the first time.



  • @cimer84

    The editTriggers for the tableView is set to QAbstractItemView::EditKeyPressed | QAbstractItemView::SelectedClicked means the createEditor function is called only when the item is already selected. So in your case the first click will select and the second click will call the createEditor(). Can you check with setting the editTriggers to QAbstractItemView::AllEditTriggers



  • @Sam the same.

    I think that the problem is with de signals that combo or something like that emit. But I don't now why the first time pop up appear and dissapear and the others only appear and I have to click one more time to dissapear.



  • Okay,

    I tested with a sample project the showPopup() of the combobox needs to be called in the setEditorData()

        {
            QString value = index.model()->data(index, Qt::EditRole).toString();
            QComboBox* comboBox = static_cast<QComboBox*>(editor);
            comboBox->setCurrentIndex(comboBox->findText(value));
    	comboBox->showPopup();
        }
    


  • @Sam I put this one in the setEditorData but the problem continue. Do you test in windows or Linux. I think that the problem is the S.O because in windows the problem is not appear.



  • @cimer84

    Yes, i test on windows 8.1 Qt 5.4.1 mvsc2010 .



  • @Sam I test with Linux with 4.8 version. can it be the problem?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.