Results 1 to 4 of 4

Thread: Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background

    Hello Guys,
    I am new to Qt , struggling a bit to get a start. Here is a situation where I want a rectangle drawn on Qmainwindow which already has its background filled with an image. Following the suggestions given on previous threads I created a widget where I drew this rectangle and later set this up as central widget.But I cudn't see any rectangle on my Qwindow. Please help me.

    Qt main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include"QWidget"
    4. #include <QtGui>
    5. #include "QPixmap"
    6.  
    7. class MyWidget : public QWidget
    8. {
    9. public:
    10. MyWidget();
    11.  
    12. protected:
    13. void paintEvent(QPaintEvent *);
    14. };
    15.  
    16. MyWidget::MyWidget()
    17. {
    18. QPalette palette(MyWidget::palette());
    19. palette.setColor(backgroundRole(), Qt::white);
    20. setPalette(palette);
    21. }
    22.  
    23. void MyWidget::paintEvent(QPaintEvent *)
    24. {
    25. QPainter painter(this);
    26. painter.setRenderHint(QPainter::Antialiasing);
    27. painter.setPen(Qt::darkGreen);
    28. painter.drawRect(1, 2, 6, 4);
    29.  
    30. painter.setPen(Qt::darkGray);
    31. painter.drawLine(2, 8, 6, 2);
    32. }
    33.  
    34.  
    35.  
    36. MainWindow::MainWindow(QWidget *parent) :
    37. QMainWindow(parent),
    38. ui(new Ui::MainWindow)
    39. {
    40. ui->setupUi(this);
    41.  
    42. setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");
    43.  
    44. MyWidget my_widget;
    45.  
    46. QWidget::setFixedSize ( 400, 400 );
    47. QMainWindow::setCentralWidget(&my_widget);
    48.  
    49. }
    50.  
    51. MainWindow::~MainWindow()
    52. {
    53. delete ui;
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sandeep_hyd123; 7th June 2011 at 00:27.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background

    But I cudn't see any rectangle on my Qwindow
    Three are couple of reasons why the widget is not visible, I have put some inline comments in the code.

    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *)
    2. {
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5. painter.setPen(Qt::darkGreen);
    6. //painter.drawRect(1, 2, 6, 4); // This is may be very small to notice, and may also get blended onto your background, using something bigger
    7. painter.drawRect(1, 2, 50, 50); // Added this
    8.  
    9. painter.setPen(Qt::darkGray);
    10. painter.drawLine(2, 8, 6, 2); // This is may be very small to notice, and may also get blended onto your background, using something bigger
    11. painter.drawLine(2, 8, 50, 50); // Added this
    12. }
    13.  
    14. MainWindow::MainWindow(QWidget *parent) :
    15. QMainWindow(parent),
    16. ui(new Ui::MainWindow)
    17. {
    18. ui->setupUi(this);
    19.  
    20. setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");
    21.  
    22. //MyWidget my_widget; // This will never work, as you are creating the widget on stack and it will be deleted as soon as you exit this function / constructor
    23. MyWidget* my_widget = new MyWidget(); // You need to create the widget on heap so that it is persistent when you exit the function / constructor
    24.  
    25. //QWidget::setFixedSize ( 400, 400 ); //You can ignore this time being
    26. //QMainWindow::setCentralWidget(&my_widget); // Has to be modified
    27. setCentralWidget(my_widget); // Added this
    28.  
    29. //Note that the widget will appear on the top left corner
    30. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Santosh Reddy for this useful post:

    sandeep_hyd123 (7th June 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background

    THat works.. thanks for those comments ..helped a lot

  5. #4
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background

    It works for me as well. Thanks indeed for the example and comments.

Similar Threads

  1. [SOLVED] QMainWindow open a new QMainWindow
    By xeroblast in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2010, 08:31
  2. Replies: 0
    Last Post: 17th November 2010, 18:07
  3. Replies: 1
    Last Post: 25th June 2010, 19:31
  4. Set background image in QMainWindow
    By superteny in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2009, 06:23
  5. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 09:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.