Beginners: set external stylesheet for qmessagebox background
-
my class inherits Qdialog. Is it the same, should I override as for QWidget?
@
Browse::Browse(QString input_filename, QString App_Path, QWidget *parent) :
QDialog(parent)
{
}
@thanks
-
For the QDialog no need to override the paintEvent().
-
It does not work.
I added the following in the style sheet editor, my class is called Browse, it is a browse window with listview and tree
@
Browse {
border: 3px solid gray;
background: QLinearGradient(x1: 0, y1: 0, x2: 0.9, y2: 0.9, stop: 0 #FF0000, stop: 1 #FFFFFF );
}
@in cpp
@
void Browse::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
@in h
@protected:
void paintEvent(QPaintEvent* event);@
-
If you are subclassing QDialog you need not override paintEvent() .
Can you paste the implementation code for your class.
Also try #Browse in your stylesheet.qss and check if it works.If you are creating an instance of your class you can also use
@yourClassInstance->setStyleSheet(styleSheet.readAll());@ -
@w->setstylesheet()@
w is the instance. And use -> , and not . (dot) operator for the pointer to the instances.
-
It does not work
@
Browse * w = new Browse(dB_Users, AppPath);
w->setObjectName("myBrowse");
Browse.setStyleSheet(styleSheet.readAll());
int ret=w->exec();
@I tried the following but it does not work either. (Qwidget.style sheet does not have class type)
@
w->setstylesheet(stylesheet.readall());
@ -
Ok for a test application I have created a dialog that consist of a single pushButton :-
.h
@class Browse : public QDialog
{
Q_OBJECTpublic:
explicit Browse(QWidget *parent = 0);
~Browse();private:
Ui::Browse *ui;
};@.cpp
@Browse::Browse(QWidget *parent) :
QDialog(parent),
ui(new Ui::Browse)
{
ui->setupUi(this);
}Browse::~Browse()
{
delete ui;
}@main.cpp
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Browse *w = new Browse();QFile styleSheet("C:/temp/style.qss"); if (!styleSheet.open(QIODevice::ReadOnly)) { qWarning("Unable to open style.qss"); return 0; } w->setStyleSheet(styleSheet.readAll()); w->show(); return a.exec();
}@
and for the style.qss file i have specified :-
@QDialog{
border: 3px solid gray;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0.6, y2: 0.6, stop: 0.6 white, stop: 1 gray);
}QPushButton {
background-color: yellow;
border: 2px solid red;
}@and here is the output :- !http://img163.imageshack.us/img163/3977/capturehug.png(text)!
"Check here":http://imageshack.us/photo/my-images/163/capturehug.png/
-
Thank you SAM but the difference from my application is that Browse is created from a login class and not from the main window. Could you please test it?
- main
- login
- browse
- login
Stylesheet of login works. But for browse not. Also, I don't use a style sheet file, I use the style sheet editor when i open login form and right click, select change style. does this make a difference?
- main
-
Just for the test , you can use write the required styles to a .qss file and read the file and use setStyleSheet() .
Also for the approach that you are using through the Qt Designer. Try to use
@QDialog {
background-color: red
}@or
@#Browse {
background-color: red
}@but then do not use setStyleSheet() inside the code otherwise it will overwrite the above.
-
Thats what I made, but it does not work on my MAC. I don't use setstylesheet but when I used it once, it worked. Setstylesheet will hardcode it.
-
I found the mistake . Thank you guys
@
Browse * w = new Browse(dB_Users, AppPath);
@should have been
@
Browse * w = new Browse(dB_Users, AppPath,this);
@ -
Great!!!
Kindly Edit your first post and prepend [Solved] to it.
Happy Coding.