linsek
linsek

Reputation: 3480

Qt - Displaying Graphics in an Application

I have an application running in Qt that I am essentially trying to re-skin. I was provided graphics to use for the background, buttons, etc. There are no current graphics in the project. It does not have a background and is just using the default look for QPushButtons. I am using Qt's UI Design editor because it will not let me just modify the mainwindow.ui file outside of the editor.

So I was able to set the background of the application by applying a stylesheet to the MainWindow object. background-image:url("/path/to/bg.png") and that worked. Cool. But now all of the QLabels in the application have the exact same background. It seems that all of the members of a QWidget inherit the stylesheet information of their parent. How do I stop that?

The other thing I am experiencing is that I need to display graphics in random places on the application. The only way I have successfully done this is by adding another QWidget and setting the stylesheet to that. Is that a proper method? What is the best way to display a non-interactive image?

Thanks

Upvotes: 0

Views: 237

Answers (1)

user336063
user336063

Reputation:

Add the stylesheet to the QApplication and be specific about what QWidgets you want to style.

Here's what you're looking for:

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow wnd;

    QString style("QMainWindow{ background: red;} QPushButton{ background: blue }");

    app.setStyleSheet(style);

    QPushButton *btn = new QPushButton("Hello", &wnd);

    wnd.show();

    return app.exec();
}

Upvotes: 1

Related Questions