PDA

View Full Version : ReImplement showPopup for my QComboBOx



ber0y
16th July 2009, 10:14
Hi,

I think that the problem is easy, but i don't know how to process for this:
I just want to reimplement QComboBox::showPopup() in my new class that inherit from QComboBox, but I want to reuse the given code and just add one line.
I can't because:



`QComboBoxPrivate* QComboBox::d_func()' is private
mycombobox.cpp:24: error: within this context
mycombobox.cpp:31: error: invalid use of undefined type `struct QAbstractItemView'
d:/Outils/Qt/qt/qt/include/QtGui/../../src/gui/itemviews/qabstractitemdelegate.h:59: error: forward declaration of `struct QAbstractItemView'
mycombobox.cpp:31: error: invalid use of undefined type `struct QComboBoxPrivate'
d:/Outils/Qt/qt/qt/include/QtGui/../../src/gui/widgets/qcombobox.h:60: error: forward declaration of `struct QComboBoxPrivate'
mycombobox.cpp:32: error: `QItemSelectionModel' has not been declared
mycombobox.cpp:32: error: `ClearAndSelect' was not declared in this scope
mycombobox.cpp:33: error: invalid use of undefined type `struct QComboBoxPrivate'
...


My code:



#include <QComboBox>
class MyComboBox : public QComboBox
{
public:
MyComboBox (QWidget *parent);
virtual void showPopup();
};

/*!
Displays the list of items in the combobox. If the list is empty
then the no items will be shown.
If you reimplement this function to show a custom pop-up, make
sure you call hidePopup() to reset the internal state.
*/
void MyComboBox::showPopup()
{
// i just try this part of the code
Q_D(QComboBox);
if (count() <= 0)
return;

QStyle * const style = this->style();

view()->selectionModel()->setCurrentIndex(d->currentIndex,
QItemSelectionModel::ClearAndSelect);

QComboBoxPrivateContainer* container = d->viewContainer();

/* ... */


How could i reuse the code for this method please ? :confused:

Thanks.

nish
16th July 2009, 11:23
you can not use it.

wysota
16th July 2009, 11:25
In general you can't. You should use QComboBox::view() and QComboBox::setView() instead.

ber0y
16th July 2009, 12:55
In general you can't. You should use QComboBox::view() and QComboBox::setView() instead.

Thanks !!! ;)

I just use view() :


void MyComboBox::showPopup()
{
view()->move(view()->x(), view()->y() - view()->height()/2);
QComboBox::showPopup();
}

I reimplement the method becasue I would like to show the items 1, 2 and 3 in a view above the QComboBox button, and the items 4, 5 and 6 below the button in a other view.
How could i do that ? : create two QListWidget and separate the QComboBox view in the two QListWidget ? :confused:

Thanks again

wysota
16th July 2009, 14:52
I think all you need to do is to move the popup up (change its geometry) using something like:


view()->window()->move(...);

sedi
8th April 2012, 12:42
It's an old thread, but the problem is still there. I needed some time to see the obvious: you have to call the base class "showPopup" before moving the popup item. This works well:


void MyQComboBox::showPopup() {
QComboBox::showPopup();
QWidget *popup = this->findChild<QFrame*>();
popup->move(popup->x(),popup->y()-this->height()-popup->height());
}