qt - Set background of QMainWindow central widget -
using qt 4.8.4 on windows 7 (msvc 2010) have standard qmainwindow
in app toolbar. want toolbar stay grey, central widget should have white background. calling centralwidget->setstylesheet("background-color: white;")
@ first seemed job, using designer-generated widget (a q_object
) doesn't. subsequently toyed around various other methods set style sheet (also using designer) no avail.
to see effect, add or remove q_object
line in test.h
. when it's there, label gets white bg. if q_object
commented out, whole central widget white. of course, want whole area white, need q_object
.
here's files:
main.cpp:
#include "test.h" class testwin : public qmainwindow { public: qwidget *centralwidget; qtoolbar *maintoolbar; testwin(qwidget *parent = 0) : qmainwindow(parent) { centralwidget = new test(this); setcentralwidget(centralwidget); maintoolbar = new qtoolbar(this); this->addtoolbar(qt::toptoolbararea, maintoolbar); }; ~testwin() {}; }; int main(int argc, char *argv[]) { qapplication a(argc, argv); testwin w; w.centralwidget->setstylesheet("background-color: white;"); w.show(); return a.exec(); }
test.h:
#include <qtgui> class test : public qwidget { q_object // remove public: qlabel *label; test(qwidget *parent = 0) { resize(400, 300); label = new qlabel(this); label->settext("test"); }; };
status update:
setstylesheet("qwidget { background-color: white; }")
not solve issue- i succeed in making every widget (including popup dialogs) white, that's not want.
ok, proper answer can found here, or alternatively reading documentation. need implement paintevent test class:
class test : public qwidget { q_object // remove public: qlabel *label; test(qwidget *parent = 0) { resize(400, 300); label = new qlabel(this); label->settext("test"); }; void paintevent(qpaintevent *) { qstyleoption opt; opt.init(this); qpainter p(this); style()->drawprimitive(qstyle::pe_widget, &opt, &p, this); } };
also many 1+1=2 read manual me @ the qt project forum.
Comments
Post a Comment