PDA

View Full Version : Paint a rectangle on QMainwindow , Qmainwindow has an image setup as background



sandeep_hyd123
7th June 2011, 00:04
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:

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Qt mainwindow.cpp:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QWidget"
#include <QtGui>
#include "QPixmap"

class MyWidget : public QWidget
{
public:
MyWidget();

protected:
void paintEvent(QPaintEvent *);
};

MyWidget::MyWidget()
{
QPalette palette(MyWidget::palette());
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
}

void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkGreen);
painter.drawRect(1, 2, 6, 4);

painter.setPen(Qt::darkGray);
painter.drawLine(2, 8, 6, 2);
}



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");

MyWidget my_widget;

QWidget::setFixedSize ( 400, 400 );
QMainWindow::setCentralWidget(&my_widget);

}

MainWindow::~MainWindow()
{
delete ui;
}

Santosh Reddy
7th June 2011, 04:45
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.



void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkGreen);
//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
painter.drawRect(1, 2, 50, 50); // Added this

painter.setPen(Qt::darkGray);
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
painter.drawLine(2, 8, 50, 50); // Added this
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");

//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
MyWidget* my_widget = new MyWidget(); // You need to create the widget on heap so that it is persistent when you exit the function / constructor

//QWidget::setFixedSize ( 400, 400 ); //You can ignore this time being
//QMainWindow::setCentralWidget(&my_widget); // Has to be modified
setCentralWidget(my_widget); // Added this

//Note that the widget will appear on the top left corner
}

sandeep_hyd123
7th June 2011, 07:12
THat works.. thanks for those comments ..helped a lot

maly
13th November 2011, 09:12
It works for me as well. Thanks indeed for the example and comments. :)