I want to add a background image to a Qt Widget. I am using Qt Creator 4.0.2 on Linux 64-bit.
The issue is when I am choosing a background image, it doesn't show (although a part of image can be seen around the PushButtons I have on page) when I run the program. However when choosing a background color works perfect.
(Using CSS)
Here are the screens :
With background-image
With background-color
Try and set it through the style sheet:
yourWidget->setStyleSheet("background-image: url(<path-to-image>/image.png)")
It works for me.
Related
I have a Qt app that runs on macOS. I found a way to change the color of the titlebar here, however I want to take it a step further. I want to mimic the titlebar that the Slack and Discord apps use. For example:
As you can see, the color of the controls in the window extend to the very top of the app's window. I figure there are two ways to accomplish what I want:
I can build on the code pasted above. Looking through some of the Apple developer documentation, I think I can create a couple NsWindows on top of the titlebar with whatever width I want and attach the titlebar as a parent for those windows. Once I do that I should be able to make the same backgroundColor() color call for each one. Of course, this will require me to keep track of when the controls or window are resized and adjust the NsWindows of the titlebar, and I am not sure what (if any) issues that could cause.
Maybe there is a way to essentially set the height of the titlebar to 0? I wonder if that's what the Discord app is doing because:
if you look closely, the edit box that says "Find or start a conversation" is vertically lined with the close, minimize and maximize buttons, as is the "Activity" label. But if the controls do extend to the top of the app's window then how are the standard app buttons getting painted?
I'd be curious to know how Slack and Discord accomplish this even though I know they're not using Qt.
I realize there is not a Qt solution since Qt does not paint the titlebar. I know this will be OS-specific, but since I do not have any real experience with Objective-C++ or working with Cocoa (all of my programming experience on macOS has been standard C++ with non-UI or Qt-based code) I'd appreciate any suggestions or guidance!
Natively this is done with fullSizeContentView and titlebarAppearsTransparent properties of NSWindow. Once you set them to true, you can draw or place controls beneath the title bar.
I've developed an application which uses qscintilla as a text editor. I also implemented custom lexer to highlight language specific keywords. So far styles for highlighted keywords are hardcoded in mine application and it looks quite ok in the default theme on Windows/Linux(Ubuntu)/Mac.
The problems appears when user chooses a dark theme (on Linux). Depending on QScintilla version some parts of editor do reflect current theme colors while other don't. Moreover mine custom styles render dark blue letters on dark grey background.
I'm looking for some Qt class, which will allow me access of the current system theme. I do not want to define styles for mine application widgets.
I want to know what is system default non-proportional font, what is it's size, color, ... If I knew that dark scheme is used I would choose complementary colors for keyword highlighting.
I checked docs for QStyle, QPlatformTheme and other qt classes and it seems to me that these serve more for defining of new styles, then for describing the current style.
For the system colours, you can use the group/role of the QPalette class.
For the system fonts, you can create a QFont using e.g. "Serif", "Sans Serif", "Monospace", etc with an appropriate style hint to discover the defaults.
NB:
From the Qt Docs:
Warning: Some styles do not use the palette for all drawing, for
instance, if they make use of native theme engines. This is the case
for both the Windows Vista and the macOS styles.
Here is some python code using QPalette that works for me on Linux:
label = QLabel("am I in the dark?")
text_hsv_value = label.palette().color(QPalette.WindowText).value()
bg_hsv_value = label.palette().color(QPalette.Background).value()
dark_theme_found = text_hsv_value > bg_hsv_value
I am using KDE theme Breeze Dark. Since all my Qt applications have changed its colors. Earlier my applications wasn't altered by KDE themes but now I am receiving message: "kf5.kiconthemes: "Theme tree: (Breeze)". I was changing background colors in my apps using stylesheets. Now due to changed fonts colors it is impossible to read (white text on very bright background). What is more I am not able to get default background color.
qDebug() << qApp->styleSheet(); gives me empty string.
How to get background color? I would like to use it setting my own backgrounds (showing priority of a task, little more reddish background and so on).
You can access to default theme colors via QPalette.
If you want just to change colors of your widgets - QSS is overkill. QPalette is more than enough.
I am using Emscripten-qt to compile Native Qt apps into Javascript. The application is simple and works fine. The only problem I am having is that the Canvas has a green background that displays behind the Window. I want to know how I can change that green background to a different color in order to stick with my color scheme.
Thanks..
mike
I'm using QT for C++ and I set this stylesheet code:
QFrame {
background-image: url(img.png);
}
The image doesn't show! It's located in the debug directory inside the QT directory. Why doesn't it show?
edit:
doesn't even work with the resource system
edit 2:
It shows now, but only if I use QWidget instead of QFrame. The only problem is that the image repeats when I resize the window. How do I make it stretch normally instead?
edit 3:
OK, I used JUST border-image now and it stretches. However, it's glitchy(if you move a bit fast you can go over the original image), and very slow(if you're not resizing slowly enough, it will stutter and until it manages to stretch there will be a white gap). Can't I get it to resize normally? =/
Using this stylesheet code:
QWidget {
border-image: url(:/images/img.png);
}
edit 4:
Well if I'm already at it.. what about clickable areas in images? Is that possible?
edit 5:
come on..anyone? this is important
Add your image to resources (.qrc file) of your application.
Then refer to it in your stylesheet:
QFrame
{
background-image: url(:/Style/img.png);
}
I think you should use Qt Resource System instead of having an image inside the folder. Images can also be speicified without the url function.