The stylesheet do not work in custom widget
-
hi, all.
i get a custom widget. the stylesheet work well in a test application.
just like this:
int main(int argc, char* argv[])
{
QApplication a(argc, argv);mywidget w;
w.setStyleSheet("some stylesheet");
w.show();return a.exec();
}but the stylesheet do not work in my project. i used QLabel instead of mywidget, the stylesheet work well in QLabel.
i do not know why.thanks.
-
Your custom widget should be made "style aware":http://doc.qt.nokia.com/latest/style-reference.html. Especially if you draw the widget yourself.
-
[quote author="Franzk" date="1290066386"]Your custom widget should be made "style aware":http://doc.qt.nokia.com/latest/style-reference.html. Especially if you draw the widget yourself.[/quote]
ok, thanks.
but my custom widget just like this:
//.h
class mywidget : public QWidget
{
Q_OBJECT
public:
mywidget(QWidget *parent = 0);};
//.cpp
mywidget::mywidget(QWidget *parent):QWidget(parent)
{}
and the initialization is :
my = new mywidget(this);
my->setStyleSheet(QString(stylesheet));
my->setGeometry(QRect(10, 10, 100, 100));and the stylesheet is "background-color: rgb(0, 170, 255);".
the stylesheet works well in QLabel but bad in mywidget.and in a simple application mywidget works well with the stylesheet, but bad in my project.
-
@
//.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class mywidget;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();void resizeEvent(QResizeEvent *);
private:
Ui::MainWindow *ui;mywidget* w;
};
class mywidget : public QWidget
{
Q_OBJECT
public:
mywidget(QWidget *parent = 0);protected:
void paintEvent(QPaintEvent *);
};#endif // MAINWINDOW_H
//.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QPainter>
const char wstyleSheet[] = "QWidget#myWidget{ background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255)); }";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);w = new mywidget(this); w->setObjectName("mywidget"); w->setStyleSheet(QString(wstyleSheet)); w->setGeometry(rect());
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::resizeEvent(QResizeEvent * e)
{
QMainWindow::resizeEvent(e);
w->setGeometry(rect());
}mywidget::mywidget(QWidget *parent):QWidget(parent)
{}
void mywidget::paintEvent(QPaintEvent * e)
{
QWidget::paintEvent(e);//test QPainter p(this); QRect t(rect()); t.adjust(50, 50, -50, -50); p.fillRect(t, QColor(255,255, 0));
}
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);// mywidget w;
// w.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255));");
MainWindow w;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endifreturn a.exec();
}
@ -
-
I did some modifications to your code, the basic idea is: take a look at ui_mainwindow.h( I'm guessing that you used MainWindow class generated by QT Creator, like an advice, try to create your own classes from scratch, it's easy to "control" your code this way ), there you will see that your mainWindow has a central widget on it, so your widget should be a child of the central widget, also I added a layout to the centralWidget control, but the strange part that I removed Q_OBJECT macro from your class and then the stylesheet worked, I didn't understand way because I used the same macro when I implemented some custom widget in the past for a qmainwindow, the only difference is that I implemented from scratch a class derived from qmainwindow
@//h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include<QHBoxLayout>namespace Ui {
class MainWindow;
}class mywidget;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget parent = 0);
~MainWindow();
QHBoxLayout ddd;private:
Ui::MainWindow ui;
mywidget xxx;
};class mywidget : public QWidget
{public:
mywidget(QWidget *parent = 0);};
#endif // MAINWINDOW_H
//cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QPainter>
#include<QPushButton>
#include<QLabel>const char wstyleSheet[] = "QWidget#myWidget{ background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255)); }";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);ddd = new QHBoxLayout; centralWidget()->setLayout( ddd ); QLabel* sss = new QLabel( centralWidget() ); sss->setMinimumSize( QSize( 10,50) ); sss->setStyleSheet( "QLabel{ background-color : red; }"); xxx = new mywidget( centralWidget() ); ddd->addWidget( xxx ); ddd->addWidget( sss );
}
MainWindow::~MainWindow()
{
delete ui;
}mywidget::mywidget(QWidget *parent):QWidget(parent)
{
setObjectName("myWidget");
setStyleSheet("QWidget#myWidget{ background-color : blue }");
setMinimumSize( QSize( 50,100) );
setMaximumSize( QSize( 50,100) );
setVisible( true );
setWindowTitle( " test ");
}/*void mywidget::paintEvent(QPaintEvent * e)
{
QWidget::paintEvent(e);//test QPainter p(this); QRect t(rect()); t.adjust(50, 50, -50, -50); p.fillRect(t, QColor(255,255, 0));
}*/
//main
#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
/*QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();*/ QApplication a(argc, argv); // mywidget w; // w.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255));"); MainWindow* w = new MainWindow ; w->show(); return a.exec();
}
@
-
[quote author="Sorin" date="1290092560"]I did some modifications to your code @[/quote]
yes, it works.
but the slot and signal are not supported without the "Q_OBJECT" marco。do you have any example that using custom widget with "Q_OBJECT" ?
thanks a lot.
-
I took a look at some code that I wrote some weeks ago, I didn't subclass my widget from QWidget, try to subclass your widget for something else relevant to your output( something that inherits QWidget like QToolButton for example ) and then it will work with Q_OBJECT, I'm going to throw a question regarding this problem on the forum, for sure we forgot to call or override a QWidget method