image - I want to show a picture in QWidget, but it doesn't work -
my code is:but can't find image in qwidget. can if want show image ? , what's wrong code?
qttest::qttest(qwidget *parent) : qwidget(parent) { qhboxlayout * m_viewlayout = new qhboxlayout(); this->setlayout(m_viewlayout); qhboxlayout * showlayout = new qhboxlayout(); qhboxlayout * btnlayout = new qhboxlayout(); m_viewlayout->addlayout(showlayout); m_viewlayout->addlayout(btnlayout); widget1 = new qwidget(); showlayout->addwidget(widget1); qpixmap image("c:\\users\\zhq\\desktop\\1.png"); qpainter painter(widget1); painter.drawpixmap(qpoint(0,0),image); widget2 = new qwidget(); showlayout->addwidget(widget2); qpushbutton * btn = new qpushbutton("btn"); btnlayout->addwidget(btn); showlayout->addstretch(); }
there's couple of thing wrong code. assuming image loaded correctly, should paint image in void qwidget::paintevent(qpaintevent * event) of widget. not paint outside of paint events. also, you're doing it, image overwritten next time widget updates/reapints anyway. might consider going easier route , use qlabel in layout instead of widget1. can call qpixmap::fromimage() convert image pixmap , qlabel::setpixmap() on label display image.
Comments
Post a Comment