Class object in mainwindow.h in Qt - c++

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.

Related

How is possible put QGraphicsView to own method as argument?

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.

Can't see a widget

I've been trying to create a frame to be shown as a tooltip, but be functionally different. It's got to be shown at a certain point, floating, to be called from a delegate.
Now, if I don't pass a parent it's seen but yeah, it's detached.
And I want it to be windowless.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>
#include <QFrame>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSize size { 350, 550 };
QPoint topLeft { 550, 550 };
auto* frame = new QFrame(this); // this is a QWidget
frame->setGeometry({ topLeft, size });
frame->show();
frame->activateWindow();
frame->raise();
qInfo() << frame->isVisible(); // returns true
}
MainWindow::~MainWindow()
{
delete ui;
}
Any suggestions?

How to send variables between classes in Qt

I have a problem. I created two classes in my Qt project. One as the main window, and second as the settings dialog. My idea is to send the values from "Settings" to "MainWindow" (like from one TextEdit to another) but unfortunately, I have no idea how to do it. It's confusing me. I have read similar topics on the internet but none of them gives me a clear answer. Can someone help me understand the way how can I do it via example?
I have no useful code to place it here, so I will put the part of the source code and headers of mine.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
[...]
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "settings.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow()
[...]
private:
Ui::MainWindow *ui;
[...]
};
#endif // MAINWINDOW_H
settings.cpp
#include "settings.h"
#include "ui_settings.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
Settings::Settings(QWidget *parent) :
QDialog(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
}
settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QDialog>
namespace Ui {
class Settings;
}
class Settings : public QDialog
{
Q_OBJECT
public:
explicit Settings(QWidget *parent = nullptr);
~Settings();
[...]
private:
Ui::Settings *ui;
[...]
};
#endif // SETTINGS_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Use signals/slots mechanism to share values between two QObject.
For example:
The following code allows yout to send the value in a QLineEdit to another widget by clicking on a button:
class Widget1: public QWidget
{
Q_OBJECT
public:
Widget1(): QWidget(),
message(new QLineEdit())
{
QPushButton* button = new QPushButton("Send msg", this);
connect(button, &QPushButton::clicked, this, [=]() { emit this->sendMsg(message->text());}); // When you click on the button, it will emit the signal sendMsg
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(message);
layout->addWidget(button);
}
private:
QLineEdit* message;
signals:
void sendMsg(QString const& msg);
};
class Widget2: public QWidget
{
Q_OBJECT
public:
Widget2(): QWidget(),
display(new QLabel("Nothing to display", this))
{
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(display);
}
private:
QLabel* display;
public slots:
void receive(QString const& message)
{
display->setText(message); // When called, display the message in the label
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* mainWidget = new QWidget();
QHBoxLayout* layout = new QHBoxLayout(mainWidget);
Widget1* w1 = new Widget1();
Widget2* w2 = new Widget2();
layout->addWidget(w1);
layout->addWidget(w2);
// When the signal sendMsg is emitted, call the slot receive
QObject::connect(w1, &Widget1::sendMsg, w2, &Widget2::receive);
mainWidget->show();
return app.exec();
}
There are multiple ways to achieve this.
For example you can provide the public Getter Methods in your dialog for provide value to the public and use them directly in the MainWindow to read those.
Or you can use Signals/Slots as stated above.
One example with Signal/Slots:
The SettingsWindow emits textEdit(QString) signal if Dialog Accepted, and MainWindow receives this signal via on_textEdit(QString) slot and writes it to its own text field:
SettingsWindow reading text input and emitting signal textEdit(QString):
MainWindow receiving signal via slot on_textEdit(QString):
And this is the code:
maindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_textEdited(QString txt);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingsdialog.h"
#include <memory>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
auto dlg = new SettingsDialog{this};
connect(dlg, &SettingsDialog::textEdit, this, &MainWindow::on_textEdited);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
}
void MainWindow::on_textEdited(QString txt)
{
ui->textEdit->setText(txt);
}
settingsdialog.h
#ifndef SETTINGSDIALOG_H
#define SETTINGSDIALOG_H
#include <QDialog>
namespace Ui {
class SettingsDialog;
}
class SettingsDialog : public QDialog
{
Q_OBJECT
public:
explicit SettingsDialog(QWidget *parent = nullptr);
~SettingsDialog();
signals:
void textEdit(QString txt);
private slots:
void on_buttonBox_accepted();
private:
Ui::SettingsDialog *ui;
};
#endif // SETTINGSDIALOG_H
settingsdialog.cpp
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
}
SettingsDialog::~SettingsDialog()
{
delete ui;
}
void SettingsDialog::on_buttonBox_accepted()
{
emit textEdit(ui->textEdit->toPlainText());
}
More about Signal/Slots

How to access other class's object in Qt?

In the program, I want to click a button in the main window and trigger the sub-window to display the image. But I cannot access ui->graphsView in the main window.cpp, how can I do that? In the main.cpp. the click-button function is assumed to do such thing. The QGraphicsView is placed in the ShowPic class.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include "showpic.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QFileSystemModel *model;
ShowPic *showpic;
QString filesPath;
};
#endif // MAINWINDOW_H
showpic.h
#ifndef SHOWPIC_H
#define SHOWPIC_H
#include <QWidget>
namespace Ui {
class ShowPic;
}
class ShowPic : public QWidget
{
Q_OBJECT
public:
explicit ShowPic(QWidget *parent = 0);
~ShowPic();
private:
Ui::ShowPic *ui;
};
#endif // SHOWPIC_H
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.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFileDialog>
#include<QFileSystemModel>
#include<QStringList>
#include <QTreeView>
#include <QGraphicsScene>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
showpic = new ShowPic();
showpic->show();
QGraphicsScene scene;
QPixmap pixmap("C:\test\\image.jpg");
scene.addPixmap(pixmap);
ui->graphicsView->setScene(&scene); ///????
}
showpic.cpp
#include "showpic.h"
#include "ui_showpic.h"
ShowPic::ShowPic(QWidget *parent) :
QWidget(parent),
ui(new Ui::ShowPic)
{
ui->setupUi(this);
}
ShowPic::~ShowPic()
{
delete ui;
}
The first thing is that you have to add the QGraphicsScene only once, and a suitable place is in the constructor:
ShowPic::ShowPic(QWidget *parent) :
QWidget(parent),
ui(new Ui::ShowPic)
{
ui->setupUi(this);
ui->graphicsView->setScene(new QGraphicsScene);
}
Then we create a method that receives the pixmap and adds it to the scene:
showpic.h
[...]
explicit ShowPic(QWidget *parent = 0);
~ShowPic();
void addPixmap(const QPixmap &pixmap);
[...]
showpic.h
[...]
void ShowPic::addPixmap(const QPixmap &pixmap){
ui->graphicsView->scene()->addPixmap(pixmap);
}
[...]
And we use that method in the slot on_pushButton_clicked:
void MainWindow::on_pushButton_clicked()
{
showpic = new ShowPic();
QPixmap pixmap("C:\test\\image.jpg");
showpic->addPixmap(pixmap);
showpic->show();
}

Using Signals and Slots to Comunicate a QDialog with MainWindow in Qt

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.