PDA

View Full Version : QTabWidget transparent background problem



destroyar0
20th June 2009, 11:01
Dear Qt experts,

I am having a problem in setting the background color of a QTabWidget to transparent.
What I would like to achieve is to have an image in the mainWindow background that shows through all the small windows on top of it.
I managed to do that with QTableView and QStackedWidget by setting the styelSheet.
For the QTabWidget instead there seems to be some additional default background which is set to white and that I cannot manage to make transparent.
Here are the settings I have tried in the styleSheet:

QString style("");
style += (QString::fromUtf8("QTableView, "));
style += QString::fromUtf8("QTabBar::tab, ");
style += QString::fromUtf8("QTabWidget, ");
style += QString::fromUtf8("QTextEdit, ");
style += QString::fromUtf8("QListView, ");
style += QString::fromUtf8("QWidget, ");
style += QString::fromUtf8("QStackedWidget, ");
style += QString::fromUtf8("QStackedLayout, ");
style += QString::fromUtf8("QRect, ");
style += QString::fromUtf8("QTableCornerButton, ");
style += QString::fromUtf8("QAbstractButton, ");
style += QString::fromUtf8("QTabWidget::pane, ");
style += QString::fromUtf8("QTabWidget::left-corner, ");
style += QString::fromUtf8("QTabWidget::right-corner, ");
style += QString::fromUtf8("QAbstractScrollArea, ");
style += QString::fromUtf8("QCheckBox, ");
style += QString::fromUtf8("QComboBox, ");
style += QString::fromUtf8("QSpinBox, ");
style += QString::fromUtf8("QFrame, ");
style += QString::fromUtf8("QGroupBox, ");
style += QString::fromUtf8("QHeaderView, ");
style += QString::fromUtf8("QLineEdit, ");
style += QString::fromUtf8("QMenu, ");
style += QString::fromUtf8("QMenuBar, ");
style += QString::fromUtf8("QProgressBar, ");
style += QString::fromUtf8("QPushButton, ");
style += QString::fromUtf8("QRadioButton, ");
style += QString::fromUtf8("QScrollBar, ");
style += QString::fromUtf8("QSizeGrip, ");
style += QString::fromUtf8("QSlider, ");
style += QString::fromUtf8("QSplitter, ");
style += QString::fromUtf8("QStatusBar, ");
style += QString::fromUtf8("QTabWidget, ");
style += QString::fromUtf8("QTabBar, ");
style += QString::fromUtf8("QTableWidget, ");
style += QString::fromUtf8("QToolBar, ");
style += QString::fromUtf8("QToolBox, ");
style += QString::fromUtf8("QToolButton, ");
style += QString::fromUtf8("QToolTip, ");
style += QString::fromUtf8("QTreeView ");
style += QString::fromUtf8("{");
style += QString::fromUtf8("background-color: rgba(255, 255, 55, 55);");
style += QString::fromUtf8("alternate-background-color: rgba(255, 255, 55, 55);");
style += QString::fromUtf8("selection-color: rgba(255, 255, 55, 55);");
style += QString::fromUtf8("selection-background-color: rgba(255, 255, 55, 55);");
style += QString::fromUtf8("background-origin: content;");
style += QString::fromUtf8("background-clip: margin;");
style += QString::fromUtf8("}");

ui->tabWidget->setStyleSheet(style);

With these settings (that are overly exagerated) I get the transparent background for the tab bar and I also get a yellow (255, 255, 55) color over a white background on the main QTabWidget area.
The very same settings work on a QTableView or QStakcedWidget.

I also tried to use:
QPalette p = ui->tabWidget->palette();
p.setBrush( QPalette::Base, QBrush( QColor::fromRgbF(255, 255, 55, 55) ) );
ui->tabWidget->setPalette(p);
which I found on another thread of this forum, but it does not change anything.

I should add that I am testing this on windows xp using Qt 4.5 in QtCreator 1.0.

Any help would be greatly appreciated!

aamer4yu
20th June 2009, 11:13
which I found on another thread of this forum, but it does not change anything.
I remember in some previous thread it was mention those settings dont work with windows style. Try running you application with some different style, plastique.
your_application.exe -style plastique

destroyar0
20th June 2009, 12:33
Yes that did it!
So the windows style is bugged.

Do you know if I can set the plastique style directly in the application? Or maybe even in QtDesigner?
Edit: I found it:
QStyleFactory styleFactory;
qApplication.setStyle(styleFactory.create("plastique"));

This thing has been a pain for a week, thank you so much!!

Lykurg
20th June 2009, 12:54
QStyleFactory styleFactory;
qApplication.setStyle(styleFactory.create("plastique"));
better use simply in your main function:


...
QApplication::setStyle(new QPlastiqueStyle); // or QCleanlooksStyle, or...
QApplication a(argc, argv);
...

destroyar0
22nd June 2009, 21:11
Indeed this is more compact, I moved to this approach.

Now I have one additional problem that seems really strange to me. When I run the application in my computer, where I have qt installed, it works drawing the background image in the mainWindow and the transparent windows on top of it.

I then create a directory including the QtCore4.dll, QtGui4.dll and the executable and try it on another computer with windows xp. Everything works but the background image is not displayed.
The very same directory works with the background image dispalyed on my computer though.

I have checked for additional dependencies using the Dependency Walker and it only needs those two dlls from Qt.

Do you have any idea of what could be the problem?

Lykurg
22nd June 2009, 21:17
What format is your image, you need to distribute the needed image plugins with the application. (On your computer it works because you have set the PATH to your dev-env and so the plugins are found. Dependency Walker doesn't show you that...)

destroyar0
22nd June 2009, 22:34
This is something I did not really know :)
What I do is:


QString backgroundImage = QString::fromUtf8("QMainWindow {background-image: url(");
if( !imageName.isEmpty() ) {
backgroundImage += imageName;
}
backgroundImage += QString::fromUtf8("); background-clip: content;}");
setStyleSheet(backgroundImage);

where imageName is a QString containing the name read from file so that it can be customized.
So far I have only tested it with jpg.
Where can I find documentation on the image plugins and how do I find out the files I need to include?

Thanks!

Lykurg
22nd June 2009, 23:45
Are you developing or windows or linux? And are you sure your image path is correct on the other machine? For that images I prefer it to be bundled with the executable. You may wanna have a look at the Qt Resource System, which allows you to store images inside your application.

destroyar0
23rd June 2009, 07:31
I am developing for windows xp and the image path is relative.
I do not want to store the image in the application because I want the user to be able to change it.
I have a configuration file from which I read the image name with this format:
DefaultBackgroundImage: images/DefaultBackground.jpg

where images is a directory in the same dir of the executable.

As you pointed out this should depend on the PATH set by the qtCreator, since when I uninstalled the qt sdk, the background image was not showing anymore even on my computer.

Lykurg
23rd June 2009, 11:53
As you pointed out this should depend on the PATH set by the qtCreator, since when I uninstalled the qt sdk, the background image was not showing anymore even on my computer.

No I was mentioned the PATH of your operation system which points the the Qt libraries and plugins. And your description points in the direction that you need to deploy the required image plugins with your application. Since the png format in implemented (<- don't know the right word for that) in the QtGui.dll test if the image is displayed right on both computers if you transform it to PNG. If so, you definitely know that some plugin (= the JPEG plugin) is missing.

destroyar0
25th June 2009, 13:19
Hello,

I am sorry I forgot to post the solution to this problem.

In the end all I needed to do was copying the dir "imageformats" in the dir containing the executable.
Of course the one provided with Qt contains more than I need, so I just removed all the unnecessary files.
This way it displays correctly the background image also on other computers.

I found a useful description in:http://doc.trolltech.com/qq/qq10-windows-deployment.html under plugins.

Thank you very much Lykurg for all your help!