Difference between revisions of "Creating a styled Gradient Button Bar"

From Qt Wiki
Jump to: navigation, search
(Category)
(Creating a new Project / Form Layout)
Line 13: Line 13:
 
* '''QPalette''' Qt has no direct way for setting colors to particular widgets, except using stylesheets, but as far as I know there is no support for gradients yet. So we need to make use of color roles and a customized {{DocLink|QPalette}}
 
* '''QPalette''' Qt has no direct way for setting colors to particular widgets, except using stylesheets, but as far as I know there is no support for gradients yet. So we need to make use of color roles and a customized {{DocLink|QPalette}}
 
* '''QListWidget''' We make use of the <tt>flow</tt> property to make the items flow horizontally.
 
* '''QListWidget''' We make use of the <tt>flow</tt> property to make the items flow horizontally.
 +
 +
== Creating a new Project ==
 +
 +
To make our life easy and get results fast we use QtCreator along this tutorial. So lets create a new project by calling 'File' - 'New File or Project' and select 'Qt Widgets Application'. Follow the Wizard steps and we should end up with a project skeleton containing a form, header and source for the main window.
 +
 +
== The mainwindow form ==
 +
 +
We start by modifying the <tt>mainwindow.ui</tt> form. Open the Forms folder and double click the .ui file. The form designer opens the empty main window. Since we don't need a tool bar, status bar and menu bar we get rid of them by right-click - 'Remove', either on the item directly or in the object inspector:
 +
 +
[[File:Gbb-object-inspector.png|center]]
 +
 +
Now that we have an empty form we add an item-based 'List Widget' (QListWidget) from the Widget Box and below that a normal widget (QWidget) that will later hold our stacked layout containing the tab pages. Let's give the list widget the name ''tabBar'' and the QWidget ''tabContainer''. To make the widgets resize nicely when you resize the main window we select the main window and press the 'Lay Out Vertically' button of the form designer toolbar (or use the Ctrl+L shortcut).
 +
 +
This should give us the following result:
 +
 +
[[File:Gbb-form-with-widgets.png|center]]
 +
 +
Well, that doesn't look very nice. The list widget going to display the tab icons is way too high. This is because the widget property ''maximum height'' does not limit the height for the current layout situation. So we set it to a fixed height, let's say 50:
 +
 +
[[File:Gbb-form-with-widgets-sized.png|center]]
 +
 +
Nice. That's better.

Revision as of 21:52, 26 March 2015

Please be patient and hold your edits. I am working on this: Simow (talk) 22:16, 25 March 2015 (UTC)

Introduction

Sometimes when you are just bored by the default QTabWidget and don't want to pull QML dependencies into your project to create a fancy user interface this article might be for you. With the help of QStyle, QBrush, a left-to-right QListWidget we are creating a stylish tab bar that can switch through a QStackedLayout that looks like this (only an example, everything customizable):

Gradient-tab-bar-01.png

What you learn

This tutorial makes use of different features and techniques provided by Qt:

  • Resource System The icons you see in the screenshot are loaded via the Qt Resource System
  • QBrush For creating the gradient we make use of QLinearGradient that is assigned to a QBrush.
  • QPalette Qt has no direct way for setting colors to particular widgets, except using stylesheets, but as far as I know there is no support for gradients yet. So we need to make use of color roles and a customized QPalette
  • QListWidget We make use of the flow property to make the items flow horizontally.

Creating a new Project

To make our life easy and get results fast we use QtCreator along this tutorial. So lets create a new project by calling 'File' - 'New File or Project' and select 'Qt Widgets Application'. Follow the Wizard steps and we should end up with a project skeleton containing a form, header and source for the main window.

The mainwindow form

We start by modifying the mainwindow.ui form. Open the Forms folder and double click the .ui file. The form designer opens the empty main window. Since we don't need a tool bar, status bar and menu bar we get rid of them by right-click - 'Remove', either on the item directly or in the object inspector:

Gbb-object-inspector.png

Now that we have an empty form we add an item-based 'List Widget' (QListWidget) from the Widget Box and below that a normal widget (QWidget) that will later hold our stacked layout containing the tab pages. Let's give the list widget the name tabBar and the QWidget tabContainer. To make the widgets resize nicely when you resize the main window we select the main window and press the 'Lay Out Vertically' button of the form designer toolbar (or use the Ctrl+L shortcut).

This should give us the following result:

Gbb-form-with-widgets.png

Well, that doesn't look very nice. The list widget going to display the tab icons is way too high. This is because the widget property maximum height does not limit the height for the current layout situation. So we set it to a fixed height, let's say 50:

Gbb-form-with-widgets-sized.png

Nice. That's better.