Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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; 6th June 2011 at 23:27.

Similar Threads

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