I\'m using Qt and I have a QTabWidget setup in the Qt Designer Editor, you can see it in picture 1.
As you can see after Tab4 there is an empty space all t
Both expanding tabs and coloring the background can be accomplished using style sheets.
For expanding tabs, a style sheet can be applied to the tabs which sets their width to a fraction of the total width of the QTabWidget
. Since the style sheet will need to be updated upon resize, it is applied using an event filter. See first example code below.
Although the background of the tab bar can be set, the tab bar doesn't fill the entire space above the tab pane. It is the container (or parent widget) which is showing through. To control the coloring of that area, put the QTabWidget
in a QWidget
and set the style sheet on the container. See second example code below.
Expanding tabs:
#include
// Sets the style sheet of the QTabWidget to expand the tabs.
static void expandingTabsStyleSheet(QTabWidget *tw)
{
tw->setStyleSheet(QString("QTabBar::tab { width: %1px; } ")
.arg(tw->size().width()/tw->count()));
}
// On resize events, reapply the expanding tabs style sheet
class ResizeFilter : public QObject
{
QTabWidget *target;
public:
ResizeFilter(QTabWidget *target) : QObject(target), target(target) {}
bool eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::Resize)
expandingTabsStyleSheet(target);
return false;
}
};
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QTabWidget *tw = new QTabWidget;
tw->installEventFilter(new ResizeFilter(tw));
tw->addTab(new QWidget, "Tab1");
tw->addTab(new QWidget, "Tab2");
tw->addTab(new QWidget, "Tab3");
tw->show();
return app.exec();
}
Background beside tabs:
#include
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWidget *container = new QWidget;
container->setStyleSheet("background: qlineargradient( x1: 0, y1: 0, x2: 1, y2
: 0, stop: 0 black, stop: 1 blue);");
QHBoxLayout *layout = new QHBoxLayout(container);
layout->setContentsMargins(0, 0, 0, 0);
QTabWidget *tw = new QTabWidget(container);
layout->addWidget(tw);
tw->setStyleSheet(
"QTabBar::tab { background: gray; color: white; padding: 10px; } "
"QTabBar::tab:selected { background: lightgray; } "
"QTabWidget::pane { border: 0; } "
"QWidget { background: lightgray; } ");
tw->addTab(new QWidget, "Tab1");
tw->addTab(new QWidget, "Tab2");
tw->addTab(new QWidget, "Tab3");
container->show();
return app.exec();
}