[Qt-interest] Problem customizing QListView

Parta lists4pghanghas at gmail.com
Wed Jun 9 14:23:13 CEST 2010


I figured out while reading documentation that if we reimpliment paintEvent
of a scrollarea, we have to call QPainter with viewport like this

QPainter(this->viewport())

Though I didnt understand fully why there is viewport() and setViewport()
when scrollarea itself has QPaintDevice in its parents and why
QPainter doesnt work
on that widget.

The other problem of setting the background, it seems that its using
some combination
of QPalette::Base and QPalette::Window for drawing background. I was
able to change it by setting both.

--
Regards,
Parta

On Wed, Jun 9, 2010 at 4:14 PM, Parta <lists4pghanghas at gmail.com> wrote:
> Hi
>
> I want to draw a custom background and some other overlays for my
> custom QListView.
> But I am not able to get rid of the white backgound that is default in
> Qt-widgets or draw anything of my own.
> I tried changing the palette which didnt work.
> I tried reimplimenting the paint but that also doesn't work instead it
> gives me a
> warning while running the application
> "QPainter::begin: Widget painting can only begin as a result of a paintEvent"
> I dont understand why it gives that warning, as far as I know I am in
> the right paintEvent
> and trying to draw onto a valid paintDevice.
>
> I also tried making the paintevent opaque by using
> "Qt::WA_OpaquePaintEvent". So that Qt doesn't draw anything on its
> own.
> But even that doesnt have any effect.
>
> I am able to make the widget transparent though by using
> setStyleSheet("background: transparent")
>
>
> Here is the pseudo code with all the methods that I tried to use to
> get my own background.
>
> class CustomListView : public QListView
> {
>
> public:
>    explicit CustomListView(QWidget *parent = 0)
>    {
>        // these are not required if the QPalette::window or paintEvent work
>        // but just to show that I have tried these, I included these lines
>        this->setAutoFillBackground(false);
>        this->setAttribute(Qt::WA_NoSystemBackground);
>        this->setAttribute(Qt::WA_OpaquePaintEvent);
>    }
>
> protected:
>    virtual void paintEvent(QPaintEvent *e)
>    {
>        QPainter painter(this);
>        painter.fillRect(rect(), QColor(0,0,0,180));
>    }
> };
>
>
> main()
> {
>    QApplication app(argc, argv);
>
>    QPalette pal = app.palette();
>    pal.setColor(QPalette::Window, QColor(20,20,20,100));
>    app.setPalette(pal);
>
>    CustomListView view;
>    view.setGeometry(100,100,200,200);
>    view.show();
>
>    return app.exec();
> }
>
> Any help will do, I am badly stuck on this one.
>
> --
> Thanks in Advance,
> Parta
>




More information about the Qt-interest-old mailing list