So, I have the source code for a training. Where I want to do operation with QGraphicsView named pp in the own class ProcessPicture.
I would ask you, is it possible to put the QGraphicsView object to the method of own class? If yes, please tell me how or lead me on answer.
For a detailed description after releasing button will done the on_processPic_pushButton_released() where is called the method of pp object. This method take a ui->graphicsview as argument. This argument will used in the pp.drawrectangle(ui->graphicsview).
The code is without error or warning, only it is not draw the rectangle to the ui->graphicsview. When I take a source code from pp.drawrectangle() and try it, in the on_processPic_pushButton_released() all is fine. So I think that i miss some knowledge.
Lets look on my source code.
processpicture.h
#ifndef PROCESSPICTURE_H
#define PROCESSPICTURE_H
#include "QString"
#include "QPicture"
#include "QGraphicsView"
class ProcessPicture
{
public:
ProcessPicture();
void draw(QGraphicsView out);
void draw_rectangle(QGraphicsView out);
private:
QString path = "***/Untitled.bmp";
QImage img;
};
#endif // PROCESSPICTURE_H
processpicture.cpp
#include "processpicture.h"
#include "QGraphicsScene"
#include "QGraphicsPixmapItem"
ProcessPicture::ProcessPicture()
{
img.load(path);
}
void ProcessPicture::draw_rectangle(QGraphicsView out){
//QGraphicsScene* scene = new QGraphicsScene();
QGraphicsScene scene;
QRect rect;
rect.setRect(10,10,10,10);
scene.addRect(rect);
out.setScene(&scene);
out.show();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "processpicture.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
ProcessPicture *pp;
//pp = new ProcessPicture;
~MainWindow();
private:
private slots:
void on_processPic_pushButton_released();
private:
Ui::MainWindow *ui;
bool event(QEvent *event);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pp = new ProcessPicture;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_processPic_pushButton_released()
{
//pp->draw(ui->graphicsView);
pp->draw_rectangle(ui->graphicsView);
}
For every non-toxic answer I thank.
Related
I try to switch between two different Widgets in QT5.
The first problem was that my first window was visible in the background of my second window. I solve this with a check in "autoFillBackground". Now i can switch between them, but if i resize them it only resize the Main content.
Both Widgets have a grid layout.
Resize Problem
Im new at QT5, so is there a better way to make a switching between 2 widgets without this problem?
I try it with wMessages = new Messages(); and hide() but then my program crash after going back.
Code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "messages.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btnMessages_clicked();
private:
Ui::MainWindow *ui;
Messages *wMessages;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnMessages_clicked()
{
wMessages = new Messages(this);
wMessages->show();
}
messages.h:
#ifndef MESSAGES_H
#define MESSAGES_H
#include <QWidget>
namespace Ui {
class Messages;
}
class Messages : public QWidget
{
Q_OBJECT
public:
explicit Messages(QWidget *parent = nullptr);
~Messages();
QString NewsGenerator();
private slots:
void on_bBack_clicked();
void on_pushButton_clicked();
private:
Ui::Messages *ui;
};
#endif // MESSAGES_H
messages.cpp:
#include "messages.h"
#include "ui_messages.h"
Messages::Messages(QWidget *parent) :
QWidget(parent),
ui(new Ui::Messages)
{
ui->setupUi(this);
}
Messages::~Messages()
{
delete ui;
}
void Messages::on_bBack_clicked()
{
this->close();
QWidget *parent = this->parentWidget();
parent->show();
}
Edit 1 - working Main Window Button (from G.M.´s answer):
void MainWindow::on_btnMessages_clicked()
{
wPlaner = new Planer();
QMainWindow::setCentralWidget(wPlaner);
}
Update (based on the comments):
The best solution was to use the QStackedWidget to navigate between the views.
Docu: https://doc.qt.io/qt-5/qstackedwidget.html#details
Example-Code for Button:
// return back to first view
void MainWindow::on_btnret_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
I am trying to implement an image editor with some customized image edition tools not present in the Qt image classes. When the user clicks on the image scene to perform some operation, I want the image to be instantly updated in the GUI application, showing in real time to the user the changes (drawing pixels, zooming...). The problem is that the actions to edit the image when clicking can only be done (at least to my knowledge) inside a separate class (in the example I show below, such class is called GraphicsScene), and thus I don't know how to transfer the edited image to the MainWindow class.
In short, I would like to "send" the edited image from the GraphicsScene class to the MainWindow class immediately after the user performs an action to edit the image, and then make the latter one execute the code to update the screen in real time after each user action is performed on the image.
For the sake of clarity, I next show the scheme of the code I have for now.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
private slots:
private:
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QApplication>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include "graphicsscene.h"
extern QImage Image_original, Image_modified;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_Image_triggered()
{
QDir dir;
QString filename=QFileDialog::getOpenFileName(this,
tr("Open Background"),
path,
tr("Images (*.png *.bmp *.jpg *.jpeg);;All files (*.*)"));
Image_original.load(filename);
GraphicsScene * img = new GraphicsScene( this );
img->addPixmap(QPixmap::fromImage(Image_original));
ui->preview->setScene(img);
}
In order to be able to track the coordinates when clicking, and following some suggestions around the net, I created a subclass of QGraphicsScene called GraphicsScene, whose header file is:
graphicsscene.h
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QList>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
signals:
public slots:
private:
int x, y;
};
#endif // GRAPHICSSCENE_H
Finally, to perform the image editions, the source file associated is:
graphicsscene.cpp
#include "graphicsscene.h"
#include <iostream>
using namespace std;
extern QImage Image_original, Image_modified;
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
this->setBackgroundBrush(Qt::gray);
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
QGraphicsScene::mousePressEvent(mouseEvent);
if (mouseEvent->button()==Qt::LeftButton)
{
x=mouseEvent->scenePos().x();
y=mouseEvent->scenePos().y();
}
Image_modified=some_custom_image_editing_code(Image_original, x, y);
}
Ideally, I would like to execute the following action in MainWindow after the mousePressEvent is performed:
img->addPixmap(QPixmap::fromImage(Image_modified));
ui->preview->setScene(img);
I would highly appreciate any idea.
Since you want to click the item containing the pixmap it is not necessary to overwrite the QGraphicsScene mousePressEvent method, since you could click outside the image, it is better to overwrite that method in QGraphicsPixmapItem.
Instead of using extern to access the images, in Qt is better signals and slot, but only the classes that inherit from QObject can have these attributes, unfortunately QGraphicsPixmapItem does not inherit from this, but we can use it as interface.
graphicspixmapitem.h
#ifndef GRAPHICSPIXMAPITEM_H
#define GRAPHICSPIXMAPITEM_H
#include <QGraphicsPixmapItem>
#include <QObject>
class GraphicsPixmapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit GraphicsPixmapItem(QObject *parent=0);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event);
signals:
void newPixmap(const QPixmap p);
};
#endif // GRAPHICSPIXMAPITEM_H
graphicspixmapitem.cpp
#include "graphicspixmapitem.h"
#include <QGraphicsSceneMouseEvent>
GraphicsPixmapItem::GraphicsPixmapItem(QObject * parent):QObject(parent)
{
}
void GraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPoint p = QPoint(event->pos().x(), event->pos().y());
QPixmap pix = pixmap();
if(!pix.isNull()){
QRect rect(QPoint(), pix.rect().size()/2);
rect.moveCenter(p);
QPixmap modified = pix.copy(rect);
modified = modified.scaled(pix.rect().size(), Qt::KeepAspectRatioByExpanding);
emit newPixmap(modified);
}
QGraphicsPixmapItem::mousePressEvent(event);
}
In the previous code we created the signal newPixmap, this connected it with a slot called onNewPixmap in MainWindow.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "graphicsscene.h"
#include "graphicspixmapitem.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void onNewPixmap(const QPixmap pixmap);
private:
Ui::MainWindow *ui;
GraphicsPixmapItem *item;
GraphicsScene *scene;
QPixmap original_pixmap;
QPixmap new_pixmap;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsView>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new GraphicsScene(this);
ui->preview->setScene(scene);
item = new GraphicsPixmapItem;
/*ui->preview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->preview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);*/
scene->addItem(item);
connect(item, &GraphicsPixmapItem::newPixmap, this, &MainWindow::onNewPixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString path = QDir::homePath();
QString filename=QFileDialog::getOpenFileName(this,
tr("Open Background"),
path,
tr("Images (*.png *.bmp *.jpg *.jpeg);;All files (*.*)"));
original_pixmap = QPixmap(filename);
item->setPixmap(original_pixmap);
}
void MainWindow::onNewPixmap(const QPixmap pixmap)
{
new_pixmap = pixmap;
QFile file("new_file.png");
file.open(QIODevice::WriteOnly);
new_pixmap.save(&file, "PNG");
}
In that slot as test I save the image in the folder where the executable is generated.
In your code I see that you use QImage, if you want to continue using it you can convert QPixmap to QImage with:
new_pixmap.toImage()
The complete example is found here
I don't get it.
loginview.h
#ifndef LOGINVIEW_H
#define LOGINVIEW_H
#include <QMainWindow>
#include <QDebug>
#include <QPushButton>
class LoginView : public QWidget
{
public:
QWidget *LoginViewSetup(QWidget * wdgMain);
public slots:
virtual void LogInUser();
virtual void CreateUser();
public:
QPushButton *logInBtn;
QPushButton *createBtn;
};
#endif // LOGINVIEW_H
loginview.cpp
#include "loginview.h"
QWidget *LoginView::LoginViewSetup(QWidget * wdgMain)
{
//some code
return wdgCenter;
}
void LoginView::LogInUser()
{
//some code
}
void LoginView::CreateUser()
{
//some code
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <loginview.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWidget * wdgMain;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
wdgMain = new QWidget(this);
LoginView LoginViewObj;
QWidget *wdgCenter = LoginViewObj.LoginViewSetup(wdgMain);
setCentralWidget( wdgMain );
connect(LoginViewObj.createBtn, SIGNAL (released()), &LoginViewObj, SLOT (CreateUser()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Problem:
QObject::connect: No such slot QWidget::CreateUser() in
../proj/mainwindow.cpp:16
I tried to add Q_OBJECT to the loginview.h and then rebuild project with QMAKE. After that there is no warning about slot, but buttons still not active. Program don't jump to handle of the button in debug mode.
Please, help me to understand what's wrong? I have an object of another class LoginView and I pass this object as a reciever for a slot. Why it passes through my class and searches slot in QWidget?
I am trting to open a new window (window with the cubeview view in qt3d application) whenever a button is clicked on the main window.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "cubeview.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int getid();
public slots:
void pattern1();
private:
CubeView *view;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
and mainwindow .cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <iostream>
#include "cubeview.h"
int pattern_id;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *button= new QPushButton("Pattern 1");
QObject::connect(button, SIGNAL(clicked()), this, SLOT(pattern1()));
button->show();
std::cout<<"reached mainwindow constructor"<<std::endl;
//view= new CubeView;
}
void MainWindow::pattern1()
{
pattern_id=1;
view->begin(1);
view->resize(800, 600);
view->show();
std::cout<<pattern_id<<std::endl;
}
int MainWindow::getid()
{
return pattern_id;
}
MainWindow::~MainWindow()
{
delete ui;
delete view;
}
I get a runtime error.
I hope you get what i am trying to do. Whenever i click on the push button, the cubeview view window should show up. What is the mistake i am making?
Where should I define the cubeView class object so that I can use it later.
Can I initialize it as CubeView *view= new CubeView; in a header file.
I tried to write it in the constructor of mainwindow.cpp
but i get a runtime error.
#ifndef CUBEVIEW_H
#define CUBEVIEW_H
#include "qglview.h"
#include "qgltexture2d.h"
QT_BEGIN_NAMESPACE
class QGLSceneNode;
QT_END_NAMESPACE
//! [1]
class CubeView : public QGLView
{
//! [1]
Q_OBJECT
public:
CubeView(QWidget *parent = 0);
~CubeView();
void begin(int pattern_id);
public slots:
void update_action();
protected:
void paintGL(QGLPainter *painter);
//! [2]
private:
QGLSceneNode *cube;
QGLSceneNode *cursor;
QGLTexture2D logo;
QGLTexture2D* texture;
QGLTexture2D handcursor;
};
//! [2]
#endif
//! [1] constructor, initialize the cube, cursor and camera
CubeView::CubeView(QWidget *parent)
{
//! [1] draw the paintboard
QVector3D *cube1_pos= new QVector3D(0.0,0.0,-1.5);
QGLBuilder builder1;
builder1 << QGL::Faceted;
builder1 << QGLCube(3.25);
cube = builder1.finalizedSceneNode();
cube->setPosition(*cube1_pos);
//draw the cursor
QGLBuilder cursor_builder;
cursor_builder <<QGL::Faceted;
cursor_builder <<QGLCube(0.15);
cursor=cursor_builder.finalizedSceneNode();
//camera setup
camera()->setFieldOfView(35);
camera()->setNearPlane(1);
camera()->setFarPlane(15);
//! [2] set texture for cube and cursor
//QImage image(QLatin1String(":/bluecircle.jpg"));
handcursor.setImage(QImage(QLatin1String(":/hand.jpg")));
std::cout<<"constructor called"<<std::endl;
}
I think I finally found my answer. It has nothing to to do with the definition of the cubeview class constructor. The simple principle is that
-> we need not create a class object within a loop or the program code crashes.
-> also the view.show if directly put inside a connect slot , will run into the error of an infinite loop.
Here is the answer.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <iostream>
#include "cubeview.h"
int pattern_id;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
view=0;
std::cout<<"reached mainwindow constructor"<<std::endl;
QPushButton *button= new QPushButton("Pattern 1");
QObject::connect(button, SIGNAL(clicked()), this, SLOT(pattern1()));
button->show();
else if(view!=NULL)
{
std::cout<<"view is already initialized"<<std::endl;
}
}
void MainWindow::pattern1()
{
if(view==NULL)
{
view=new CubeView;
view->begin(1);
view->resize(800, 600);
view->show();
}
}
int MainWindow::getid()
{
return pattern_id;
}
MainWindow::~MainWindow()
{
delete ui;
delete view;
}
In this way, the new object window is created if the value is already NULL.
Ok I think is time to get some help, I have been practicing with signals and slots in Qt and I got stocked. What I want is to be able to change a label in mainwindow when a button in a QDialog is clicked. I have been searching and apparently the only way to do this is basically using signals and slots, here is what I have...
I have a mainwindow.ui with a button called "pushButton_OpenWindow" and a QLabel label_ShowText", I also have a externaldialog.ui that contains a QLineEdit called lineEdit_ExternalInput" and a QPushButton called "pushButton_SendText", and what I want is to change "label_ShowText" to whatever value "lineEdit_ExternalInput" is when pushButton_SendText" is clicked but it doesn't work, when I click the button nothing happens no errors, no warnings nothing.
Here is the code that doesn't work...
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_OpenWindow_clicked();
void textValue(const QString &newText);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "externaldialog.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ExternalDialog *externalDialog = new ExternalDialog;
// connecting signals and slots
QObject::connect(externalDialog, SIGNAL(textChanged(QString)), this, SLOT(textValue(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_OpenWindow_clicked()
{
ExternalDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
}
void MainWindow::textValue(const QString &newText)
{
ui->label_ShowText->setText(newText);
qDebug()<<"Message from textValue Function \n";
}
externaldialog.h
#ifndef EXTERNALDIALOG_H
#define EXTERNALDIALOG_H
#include <QDialog>
namespace Ui {
class ExternalDialog;
}
class ExternalDialog : public QDialog
{
Q_OBJECT
public:
explicit ExternalDialog(QWidget *parent = 0);
~ExternalDialog();
private:
Ui::ExternalDialog *ui;
signals:
void textChanged(const QString&);
public slots:
void on_pushButton_SendText_clicked();
};
#endif // EXTERNALDIALOG_H
externaldialog.cpp
#include "externaldialog.h"
#include "ui_externaldialog.h"
#include <QDebug>
ExternalDialog::ExternalDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExternalDialog)
{
ui->setupUi(this);
}
ExternalDialog::~ExternalDialog()
{
delete ui;
}
void ExternalDialog::on_pushButton_SendText_clicked()
{
emit textChanged(ui->lineEdit_ExternalInput->text());
qDebug()<<"Sent Message";
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Any idea what I'm I doing wrong? Any suggestion will be appreciated. Sorry I posted the whole code but sometimes is better to see the whole picture.
Thanks a lot
change function MainWindow::on_pushButton_OpenWindow_clicked() into this
void MainWindow::on_pushButton_OpenWindow_clicked()
{
externalDialog->setModal(true);
externalDialog->exec();
}
You have just create a new unconnected dialog in the original function.