PDA

View Full Version : Change QTabWidget tabs while resizing



michael.
16th May 2011, 15:13
I would like to hide the text and just show the icons on the tabs of my QTabWidget when the user resizes the QTabWidget and hits the minimumSizeHint with the text showing.
Everything is working fine except when I hide the text I have to release the mouse and click again to continue shrinking the widget, even though I hide the text and call updateGeometry().

I tried to do this with an event filter. Is there a better way to do this and is this even possible?
I am using Qt 4.5.2.



class TabWidgetEventFilter : public QObject
{
Q_OBJECT

public:
TabWidgetEventFilter(QObject* parent, QTabWidget* tabWidget)
: QObject(parent)
{
if (!tabWidget)
return;

_tabWidget = tabWidget;

for (int i = 0; i < _tabWidget->count(); ++i)
_tabNames.append(_tabWidget->tabText(i));

_minimumSizeHintWithText = _tabWidget->minimumSizeHint().width();
}

protected:
bool eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::Resize)
{
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
if (resizeEvent->size().width() <= _minimumSizeHintWithText)
setTextVisible(false);
else
setTextVisible(true);
}

return false;
}

private:
void setTextVisible(bool visible)
{
for (int i = 0; i < _tabWidget->count(); ++i)
_tabWidget->setTabText(i, visible ? _tabNames.at(i) : QString(""));

_tabWidget->updateGeometry();
}

QList<QString> _tabNames;
QTabWidget* _tabWidget;
int _minimumSizeHintWithText;
};


Thanks for any help
Michael

wysota
17th May 2011, 01:23
What happens if you don't release the mouse?

michael.
17th May 2011, 08:12
What happens if you don't release the mouse?

I can't make it smaller than the "old" minimum size hint (the one with text). The text is already hidden and updateGeometry has been called.

wysota
17th May 2011, 11:39
Correct the minimum size hint then so that it doesn't use the text in its calculations. That's the way it should be.

michael.
17th May 2011, 12:10
I just checked the size hint and it seems to return the correct values, so the problem might be that during the resize operation (while the mouse button is pressed) Qt doesn't seem to notice that the size hint has changed and still uses an old value.

wysota
17th May 2011, 12:11
No. Correct minimumSizeHint to ALWAYS return the smaller size, not only when you reach the minimum WHILE resizing.

michael.
17th May 2011, 12:21
Ah of course, I didn't get that.
Thank you so much. :D