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[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Qt mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QWidget"
#include <QtGui>
#include "QPixmap"
{
public:
MyWidget();
protected:
};
MyWidget::MyWidget()
{
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
}
{
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
) : 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 );
}
MainWindow::~MainWindow()
{
delete ui;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks