Hi I am trying a very simple QGraghicsScene code in QT. But the addRect function is not working, as I try to add it to specific coordinate. It is always at the centre. I also have tried with setPos. But same result.
MainWindow::MainWindow(QWidget * parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui -> setupUi(this);
scene = new QGraphicsScene(this);
ui -> graphicsView -> setScene(scene);
QBrush greenBrush(Qt::green);
QBrush blueBrush(Qt::gray);
QPen outlinePen(Qt::black);
outlinePen.setWidth(2);
scene -> update();
rectangle = scene -> addRect(20,200,100,100,outlinePen,greenBrush);
scene - >update();
}
MainWindow::~MainWindow()
{
delete ui;
}
Related
Is it possible to set (0,0) to the top-left corner of the QGraphicsScene? It seems to be at the center of the view by default:
class GraphicsScene : public QGraphicsScene
{
protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
{
// here you can see that (0,0) is at the center of the view
qDebug() << "movement " << event->scenePos();
}
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
GraphicsScene* scene = new GraphicsScene;
QGraphicsView* view = new QGraphicsView(scene);
setCentralWidget(view);
}
You have to set the alignment in the QGraphicsView:
QGraphicsView* view = new QGraphicsView(scene);
view->setAlignment(Qt::AlignTop | Qt::AlignLeft);
A qsplitter has been used in my application, and OpaqueResize is set to 'false',while dragging the splitter handle, the widget becoming smaller is repainted continuously, this is not right. What I thought is that the QWidgets on both sides of the splitter handle should not receive any signals about repaint or resize until mouseRelease.This issue troubles me several days, if anyone knows about how to deal with it, please give me a hand, thank you.
Qt5.11.2 on Windows10 64-bits
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// left
ui->widget_2->bgColor = QColor(255,0,0);
ui->widget_2->tag = 0;
// right
ui->widget_3->bgColor = QColor(0,0,255);
ui->widget_3->tag = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
bgColor = QColor(255,255,255);
tag = 0;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent( event );
QPainter painter(this);
QBrush brush( bgColor );
painter.fillRect( event->rect(), brush );
qDebug() << "paintEvent:tag="<<tag;
}
I want to draw on a QMainWindow: Until now, I have set up a simple project and entered some code into the constructor. The QGraphicsView shows a blank canvas. What else do I have to do to make my drawings visible?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene scene(0,0,800,600);
this->view.setScene(&scene);
this->setCentralWidget(&view);
scene.addText("Hello, world!");
QPen pen(Qt::green);
scene.addLine(0,0,200,200,pen);
scene.addEllipse(400,300,100,100,pen);
//make a point:
double rad = 1;
scene.addEllipse(10-rad, 10-rad, rad*2.0, rad*2.0,pen,QBrush(Qt::SolidPattern));
scene.update();
view.show();
}
QGraphicsScene scene is a local variable of MainWindow constructor. Once constructor ended, the variable is destroyed, which leads to undefined behavior when the view tries to access the scene. Your program may crash.
Make it a member variable of the MainWindow class and initialize with new in constructor. Or at least try QGraphicsScene *scene = new QGraphicsScene(0,0,800,600,this), it will get deleted by Qt when MainWindow is destroyed.
I'm using the answer to this SO question to make a custom image widget which automatically scales correctly. It works fine but now I am trying to center the image widget instance at the center of my main window.
My idea was to create a QHBoxLayout, add the image widget to that and then add the hBox instance to the ui->verticalLayout.
Doesn't work. The image still displays flush left with the error message: QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout
I then tried a few variations on 'setAlignment` but then the image doesn't appear at all. My simple test code is below.
What am I missing here?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix;
pix.load("/Users/home/Desktop/test.jpg");
ImageLabel2* image = new ImageLabel2(this);
image->setPixmap(pix);
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addWidget(image);
// hbox->setAlignment(image,Qt::AlignCenter);
hbox->setAlignment(Qt::AlignHCenter);
// ui->verticalLayout->addLayout(hbox);
ui->verticalLayout->addLayout(hbox);
// ui->verticalLayout->addWidget(image);
// ui->verticalLayout->setAlignment(image,Qt::AlignCenter);
// ui->verticalLayout->setAlignment(Qt::AlignHCenter);
}
Try this:
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addStretch();
hbox->addWidget(image);
hbox->addStretch();
None of the suggestions here worked for me. Don't know why and it seems hard to debug layout problems
However a reply to my question on the Qt Project.org site works perfectly. So not my solution but I am posting it here since this "centering/resizing image" issue seems a common problem.
class CustomWidget : public QWidget {
public:
CustomWidget(const QPixmap &p, QWidget* parent = 0)
: QWidget(parent), pixmap(p) {}
void paintEvent(QPaintEvent * e)
{
QRect srcRect(QPoint(), pixmap.size());
QSize dstSize = srcRect.size().scaled(
e->rect().size(), Qt::KeepAspectRatio);
QRect dstRect(QPoint((width() - dstSize.width())/2,
(height() - dstSize.height())/2), dstSize);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.drawPixmap(dstRect, pixmap, srcRect);
}
private:
QPixmap pixmap;
};
and then in the main window:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(new CustomWidget(QPixmap("test.png")));
}
main window(QMainwindow) API setCentralWidget to align the widget on center.
Note:Concept of centrewidget, is to differentiate the docking area( Left, Right, Bottom, Top ).
with out a centre how some one know who is where. when we are developing with QMainWindow, you can see in UI_MainWindow.h, setting Centrewidget with dummy QWidget
Below code will work
#include "mainwindow.h"
#include <QLabel>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
QPixmap pix;
pix.load("C:\\Users\\user\\Desktop\\Uninstallation failure2.png");
//Replace with ImageLabel2
QLabel* image = new QLabel(this);
image->setPixmap(pix);
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addWidget(image);
QWidget* centreWidget = new QWidget();
//QMainwindow, having a feature called centreWidget, to set the layout.
centreWidget->setLayout( hbox );
setCentralWidget( centreWidget );
}
MainWindow::~MainWindow()
{
}
I'm trying to test animations in Qt desktop application. I just copied example from help. After button click, new button just appear in left top corner without animation (even end position is wrong). Am I missing something?
Qt 5.0.1, Linux Mint 64bit, GTK
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
QPropertyAnimation animation(button, "geometry");
animation.setDuration(10000);
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));
animation.start();
}
Edit: Solved. Animation object must be as global reference. For example in section private QPropertyAnimation *animation. Then QPropertyAnimation = New(....);
You don't need to make a slot specifically for deleting the mAnimation variable. Qt can do it for you if you use QAbstractAnimation::DeleteWhenStopped:
QPropertyAnimation *mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start(QAbstractAnimation::DeleteWhenStopped);
You just didn't copied the example, you also made some changes that broke it. Your animation variable is now a local variable that is destroyed at the end of on_pushButton_clicked function. Make the QPropertyAnimation instance a member variable of the MainWindow class and use it like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), mAnimation(0)
{
ui->setupUi(this);
QPropertyAnimation animation
}
MainWindow::~MainWindow()
{
delete mAnimation;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start();
}