PDA

View Full Version : QTabWidget custom draw and empty in designer problems.



paulz
9th December 2009, 22:12
I want to show an empty QTabWidget in the designer but it seems this is not possible, so I have to remove it in code?

After removing it in code I get an empty tab widget where I would like to display some text in a label in the middle of the tab widget. My question is how can I achieve this?

paulz
10th December 2009, 22:31
I managed to figure this out, it seemed pretty simple.

customtab.hpp


#ifndef CUSTOMTAB_HPP
#define CUSTOMTAB_HPP

#include <QTabWidget>
#include <QLabel>

class CustomTab : public QTabWidget
{
public:
CustomTab( QWidget* aParent = 0 );
~CustomTab();
virtual void tabInserted( int aIndex );
virtual void tabRemoved( int aIndex );
virtual void resizeEvent(QResizeEvent* aEvent );
protected:
void SetLabelPosition();
private:
QLabel* iLabel;
};

#endif // CUSTOMTAB_HPP



customtab.cpp


#include "customtab.hpp"
#include <QTabBar>

CustomTab::CustomTab( QWidget* aParent )
: QTabWidget( aParent ), iLabel( NULL )
{
iLabel = new QLabel( this );
iLabel->setText( "Double click on a file segment to open it." );
if (count())
{
iLabel->setVisible(false);
}
iLabel->setWordWrap(true);
SetLabelPosition();
}

CustomTab::~CustomTab()
{
delete iLabel;
}

void CustomTab::tabInserted( int aIndex )
{
if ( count() )
{
iLabel->setVisible(false);
}
}

void CustomTab::tabRemoved( int aIndex )
{
if ( !count() )
{
iLabel->setVisible(true);
}
}

void CustomTab::resizeEvent( QResizeEvent* aEvent )
{
QTabWidget::resizeEvent( aEvent );
SetLabelPosition();
}

void CustomTab::SetLabelPosition()
{
int xpos = width() /2 - iLabel->width() / 2;
int ypos = height() /2 - iLabel->height() /2;
iLabel->move( xpos, ypos );
}

// End of file