How to create object widget in other widgets in QT - c++

In my application im having three widgets named as "Widget" , "one" and "two" . i tried to create the objects of the widget in main function as pass it as an argument to another widgets . it compiles successfully but application crashed before running , refer my code below and guide me,
//main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "one.h"
#include "two.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w;
One *one;
Two *two;
w->GetObject(one,two);
one->GetObject(w,two);
two->GetObject(one,w);
w->show();
return a.exec();
}
//widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void Widget::GetObject(One *onee, Two *twoo)
{
one=onee;
two=twoo;
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "ui_widget.h"
class One;
class Two;
class Widget : public QWidget, private Ui::Widget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
One *one;
Two *two;
void GetObject(One *onee, Two *twoo);
};
#endif // WIDGET_H
//one.cpp
#include "one.h"
One::One(QWidget *parent) :QWidget(parent)
{
setupUi(this);
}
void One::GetObject(Widget *widgett, Two *twoo)
{
widget = widgett;
two = twoo;
}
void One::on_pushButton_clicked()
{
widget->show();
}
//one.h
#ifndef ONE_H
#define ONE_H
#include "ui_one.h"
class Widget;
class Two;
class One : public QWidget, private Ui::One
{
Q_OBJECT
public:
explicit One(QWidget *parent = 0);
Widget *widget;
Two *two;
void GetObject(Widget *widgett, Two *twoo);
private slots:
void on_pushButton_clicked();
};
#endif // ONE_H

Related

Qt5: error: ‘qt_metacall’ is not a member of

I try to become more familiar with singnals and slots with Qt.
I want to emit a signal in one class and want to handle it at one other.
Here my example code:
main.c
#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>
class Emiter
{
signals:
void anSignal ();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void handleEmitter ();
public:
MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
auto emiter = new Emiter();
connect( emiter,
&Emiter::anSignal,
this,
&MainWindow::handleEmitter );
}
void
MainWindow::handleEmitter()
{
}
Then I get this error:
error: ‘qt_metacall’ is not a member of ‘Emiter’ enum { Value = sizeof(test(&Object::qt_metacall)) == sizeof(int) };
What does this mean?
Signals and slots are available only in QObject derived classes, and a Q_OBJECT macro is needed.
class Emiter : public QObject
{
Q_OBJECT
public:
signals:
void anSignal ();
};
For more detail answer: click here
Another case is if you use multiple inheritances, you need to put QObject as the first parent class.

Qt make modification of MainWindow from another class

I would like to make some modifications of the main window from another file.
I created another ui file Form1Window (which open when a button is cliked in the MainWindow).
I want to call from the class Form1Window a function named test() of the MainWindow class
I succeed in calling function test() but I can't execute the whole content of the function (I can display a message but can't execute the part where I want to clear an edittext)
MainWindow.h
#include "form1window.h"
public slots:
void nettoyer();
private slots:
void openFrom1();
private:
Ui::MainWindow *ui;
From1Window *uiFrom1;
};
MainWindow.cpp
void MainWindow::openFrom1()
{
uiFrom1 = new From1Window(this);
uiFrom1->show();
}
void MainWindow::nettoyer(){
QMessageBox msgBox;
msgBox.setText("test");
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
ui->auteur->clear();
//THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS
}
form1window.cpp
#include "mainwindow.h"
#include "ui_form1window.h"
void From1Window::on_supprimer_clicked()
{
MainWindow *a=new MainWindow ();
a->test();
close();
}
I've read about the role of the pointer of MainWindow class (C++ /Qt Proper way to access ui from another class in qt //Edited) and I've also tried connect()
Thank for your help
//THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS
this->ui->auteur->clear();
The line will never executed unless you dismiss QMessageBox. This is because you triggered QMessageBox with exec() function. This function has its own event queue and does not return until finishes. You may set QMessageBox as modal and display it with show() method. In that case QMessageBox will not block execution of the the flow.
This problem can also happen with QDialog(s) if you display them with exec().
I provide you a simple two window signal/slot example:
main.cpp
#include "mainwindow.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form f;
f.show();
return a.exec();
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QPushButton>
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
private:
QPushButton *pb;
};
#endif // FORM_H
form.cpp
#include "form.h"
#include "mainwindow.h"
#include <QDebug>
Form::Form(QWidget *parent) : QWidget(parent)
{
MainWindow *mw = new MainWindow();
pb = new QPushButton("clickME", this);
QObject::connect(pb, SIGNAL(clicked()), mw, SLOT(test()));
mw->show();
}
Form::~Form()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void test();
private:
QLabel *l;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
l = new QLabel("test", this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::test() {
qDebug() << "test called!" << endl;
l->setText("text changed");
}
This works for me.
output

Add separate files for functions in qt creator

Hii I am new to programming i have stated learning about c++ and qt . i want to create a simple program in which user gives input from gui, these input are then send to a function (which is a separate header and cpp file) the value is evaluated in this function and again displayed in the gui.
i have three file namely main.cpp, (mainwindow.cpp & mainwindow.h), and (addition.cpp and addition.h)
i want that the values are read from mainwindow.ui (from lineedit) they are then send to the function addition.cpp and evaluated and is send back to mainwindow.cpp or mainwindow.ui (to lineedit) so that i can access this result.
here is the code i was trying
please help me in understanding the process
//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 "addition.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QObject *sumnum;
private:
Ui::MainWindow *ui;
float number1,number2;
public slots:
void results(float);
private slots:
void on_addnum_clicked();
};
#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);
sumnum = new QObject(this);
connect(sumnum,SIGNAL(add(float)),this,SLOT(results(float)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::results(float answer)
{
ui->sum->setText(QString::number(answer));
}
void MainWindow::on_addnum_clicked()
{
addresult(ui->num1->text().toDouble(),ui->num2->text().toDouble());
}
//addition.h
#ifndef ADDITION_H
#define ADDITION_H
#include <QObject>
class addition : public QObject
{
Q_OBJECT
public:
explicit addition(QObject *parent = 0);
void run(float, float);
private:
float answer;
signals:
void add(float);
public slots:
};
#endif // ADDITION_H
//addition.cpp
#ifndef ADDITION_H
#define ADDITION_H
#include <QObject>
class addition : public QObject
{
Q_OBJECT
public:
explicit addition(QObject *parent = 0);
void run(float, float);
private:
float answer;
signals:
void add(float);
public slots:
};
#endif // ADDITION_H
In your MainWindow class, the type of sumnum variable has to be addition because you'll need the complete type in order to connect to its signals/slots
sumnum class should have a public function like void add(flaot n1, float n2) so you can call it in your on_addnum_clicked function

Share data between two QWidget instances

I would like share a string between two instances of QWidget.
In main.cpp, two objects are instantiated and shown like this:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w1,w2; //Derived from QWidget
w1.show();
w2.show();
return a.exec();
}
I would introduce SharedState class:
// shared_state.h
#ifndef SHARED_STATE_HPP
#define SHARED_STATE_HPP
#include <QObject>
class SharedState : public QObject
{
Q_OBJECT
public:
SharedState(QString initialValue = "")
: currentValue(initialValue)
{}
QString getCurrentValue()
{
return currentValue;
}
public slots:
void setValue(QString newValue)
{
if(currentValue != newValue)
{
currentValue = newValue;
emit valueChanged(currentValue);
}
}
signals:
void valueChanged(QString);
private:
QString currentValue;
};
#endif // SHARED_STATE_HPP
Now I would provide reference to SharedState in Dialog's constructor,
// dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QWidget>
#include "shared_state.h"
namespace Ui {
class Dialog;
}
class Dialog : public QWidget
{
Q_OBJECT
public:
explicit Dialog(SharedState& state, QWidget *parent = 0);
~Dialog();
private slots:
void handleTextEdited(const QString&);
public slots:
void handleInternalStateChanged(QString);
private:
Ui::Dialog *ui;
SharedState& state;
};
#endif // DIALOG_H
You may have noticed that I have added two slots, one to handle the case when the text is manually edited and one when shared state will inform us that we are out of date.
Now in Dialog's constructor I had to set initial value to textEdit, and connect signals to slots.
// dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(SharedState& state, QWidget *parent) :
QWidget(parent),
ui(new Ui::Dialog),
state(state)
{
ui->setupUi(this);
ui->textEdit->setText(state.getCurrentValue());
QObject::connect(ui->textEdit, SIGNAL(textEdited(QString)),
this, SLOT(handleTextEdited(QString)));
QObject::connect(&state, SIGNAL(valueChanged(QString)),
this, SLOT(handleInternalStateChanged(QString)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::handleTextEdited(const QString& newText)
{
state.setValue(newText);
}
void Dialog::handleInternalStateChanged(QString newState)
{
ui->textEdit->setText(newState);
}
Now the change in the main function:
// main.cpp
#include "dialog.h"
#include "shared_state.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SharedState state("Initial Value");
Dialog w1(state), w2(state);
w1.show();
w2.show();
return a.exec();
}

Implementing Something like QDockWidget::togleViewAct for regular widgets

just trying to get this working, I don't understand why it doesn't work. compiled with Qt Creator on Windows 7 x64, but I don't think that should matter too much.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QAction;
class QMenu;
class QWidget;
class mywidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private:
void createActions();
void createMenus();
mywidget *thewidget;
QMenu *viewMenu;
QAction *toggleViewAct;
};
#endif
mywidget.h:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class mywidget : public QWidget
{
public:
mywidget(QWidget *parent){ m_parent = parent ; }
private slots:
void toggleView(){ isHidden() ? setVisible(true) : setVisible(false) ; }
private:
QWidget *m_parent;
};
#endif
mainwindow.cpp:
#include <QtWidgets>
#include "mainwindow.h"
#include "mywidget.h"
MainWindow::MainWindow()
{
thewidget = new mywidget(new QWidget());
thewidget->setWindowTitle("test");
thewidget->setVisible(true);
createActions();
createMenus();
}
void MainWindow::createActions()
{
toggleViewAct = new QAction(tr("Toggle other widget"), this);
toggleViewAct->setStatusTip(tr("toggle tabbed widgets visibility"));
connect(toggleViewAct, SIGNAL(triggered()), thewidget, SLOT(toggleView()));
}
void MainWindow::createMenus()
{
viewMenu = menuBar()->addMenu(tr("&Menu"));
viewMenu->addAction(toggleViewAct);
}
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("QtProject");
app.setApplicationName("Application Example");
MainWindow mainWin;
mainWin.show();
return app.exec();
}