I'm trying displaying widget 'widg' in layer on MainWindow after pushing 'pushButton_2' but I'm receive this error: "expected primary-expression before ')' token"
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "widg.h"
#include "ui_widg.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->pushButton_2, SIGNAL(clicked()), SLOT(slotPush2()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotPush2()
{
ui->verticalLayout_3->addWidget(widg);
}
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:
Ui::MainWindow *ui;
private slots:
void slotPush2();
};
#endif // MAINWINDOW_H
widg.h
#ifndef WIDG_H
#define WIDG_H
#include <QWidget>
namespace Ui {
class widg;
}
class widg : public QWidget
{
Q_OBJECT
public:
explicit widg(QWidget *parent = 0);
~widg();
private:
Ui::widg *ui;
};
#endif // WIDG_H
widg.cpp
#include "widg.h"
#include "ui_widg.h"
widg::widg(QWidget *parent) :
QWidget(parent),
ui(new Ui::widg)
{
ui->setupUi(this);
}
widg::~widg()
{
delete ui;
}
Please, help me, what is my mistake?
It's difficult to understand precisely what your intentions are, but perhaps you meant this:
void MainWindow::slotPush2()
{
ui->verticalLayout_3->addWidget(new widg(this));
}
Related
My app, consists in 2 different object (QObject and QMainWIndow), and I am wondering how to communicate between them with SLOT/SIGNAL. Moreover, does existing better approach ?
Can someone make an simple example ? Thank :)
sample
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
#include "object.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "object.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
}
MainWindow::~MainWindow()
{
delete ui;
}
object.h
#ifndef OBJET_H
#define OBJET_H
#include "mainwindow.h"
#include <QMainWindow>
#include <QObject>
class Object : public QObject
{
Q_OBJECT
public:
explicit Object(QObject *parent = 0);
bool state;
signals:
private slots:
void chkState(Ui::MainWindow *ui);
};
#endif // OBJET_H
objet.cpp
#include "object.h"
#include "mainwindow.h"
Object::Object(QObject *parent) : QObject(parent)
{
}
void Object::chkState(Ui::MainWindow *ui)
{
if (ui->chkState->isChecked())
{
ui->state->setText("true");
state = true;
}
else
{
ui->state->setText("false");
state = false;
}
}
Here is a simple example of how to emit signals and slots.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "object.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void transmit_to_object(bool value);
private slots:
void receive_from_object(bool value);
void on_checkBox_clicked();
private:
Ui::MainWindow *ui;
object m_object;
};
#endif // MAINWINDOW_H
#ifndef OBJECT_H
#define OBJECT_H
#include <QObject>
class object : public QObject
{
Q_OBJECT
public:
explicit object(QObject * parent = 0);
signals:
void transmit_to_gui(bool value);
private slots:
void receive_from_gui(bool value);
private:
bool state;
};
#endif // OBJECT_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m_object,SIGNAL(transmit_to_gui(bool)),this,SLOT(receive_from_object(bool)));
connect(this,SIGNAL(transmit_to_object(bool)),&m_object,SLOT(receive_from_gui(bool)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::receive_from_object(bool value)
{
if(value)
{
ui->lineEdit->setText("true");
}
else
{
ui->lineEdit->setText("false");
}
}
void MainWindow::on_checkBox_clicked()
{
if(ui->checkBox->isChecked())
{
emit transmit_to_object(true);
}
else
{
emit transmit_to_object(false);
}
}
#include "object.h"
#include "mainwindow.h"
object::object(QObject *parent)
{
}
void object::receive_from_gui(bool value)
{
state = value;
emit transmit_to_gui(state);
}
There are several errors in your code.
First:
connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
Here you say:
"when we click on the ui->chkState, I want you call a function in this, which is the object->chkState slot". That's definitly not what you want.
What is object ? this object hasn't been created. What you want is :
connect(ui->chkState, SIGNAL(clicked()), myobject, SLOT(chkState()));
with myobject an object of type Object so you need to add in your mainwindow.h a
Object *myobject;
and in your mainwindow.cpp before the connect:
myobject = new Object(this);
Moreover, your function void chkState(Ui::MainWindow *ui); won't work because you cannot get the mainwindow ui in parameter like that.
What I advise you to do, if it's only for tests so you know that parent is the type of MainWindow, you can do:
void Object::chkState()
{
MainWindow* parent = static_cast<MainWindow*>(parent());
if (parent->ui->chkState->isChecked())
{
parent->ui->state->setText("true");
state = true;
}
else
{
parent->ui->state->setText("false");
state = false;
}
}
So the parameter in your slot is removed.
Welcome, I have a question about Qt's windows operations. I have a simple app which contains 2 windows:
MainWindow - includes push button, can't be resizeable,
AdminWindow - includes label, can be resizeable.
When I click push button, it should open AdminWindow and hide MainWindow. I made the app and it seems to work but when I open the AdminWindow, the application icon which is located in windows's bottom bar is missing. How can I fix it?
Icon is showed when MainWindow is opened:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "adminwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonOpenAdmin_clicked();
private:
Ui::MainWindow *ui;
AdminWindow *adminWindow;
};
#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;
}
// hides MainWindow and show AdminWindow
void MainWindow::on_pushButtonOpenAdmin_clicked()
{
hide();
adminWindow = new AdminWindow(this);
adminWindow->show();
}
adminwindow.h
#ifndef ADMINWINDOW_H
#define ADMINWINDOW_H
#include <QMainWindow>
namespace Ui {
class AdminWindow;
}
class AdminWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AdminWindow(QWidget *parent = 0);
~AdminWindow();
private:
Ui::AdminWindow *ui;
};
#endif // ADMINWINDOW_H
adminwindow.cpp
#include "adminwindow.h"
#include "ui_adminwindow.h"
AdminWindow::AdminWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminWindow)
{
ui->setupUi(this);
}
AdminWindow::~AdminWindow()
{
delete ui;
}
I want to return a QString value from a function that exists in class called DB in my MainWindow class, but it always crashes the application.
db.h
#ifndef DB_H
#define DB_H
#include <QMainWindow>
#include <QtSQl>
#include <QSqlQuery>
#include <QSqlError>
#include <QObject>
#include <QDialog>
class DB : public QMainWindow
{
Q_OBJECT
public:
explicit DB(QWidget *parent = 0);
QString getDriver() const;
void setDriver(const QString &value);
private:
QString Driver="test";
signals:
public slots:
};
#endif // DB_H
and this is db.cpp
#include "db.h"
DB::DB(QWidget *parent) : QMainWindow(parent)
{
}
QString DB::getDriver() const
{
return Driver;
}
void DB::setDriver(const QString &value)
{
Driver = value;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QFileInfo>
#include <QDialog>
#include <QObject>
#include "Db.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DB *conn;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString driverfromdb = conn->getDriver();
qDebug() << driverfromdb;
}
And this what happen when I click the pushbutton: image
You are declaring a pointer to DB class inside your MainWindow class
DB *conn;
but you never initialize it. At the moment it does not point to anything, so if you try to access it:
QString driverfromdb = conn->getDriver(); // boom, conn is somewhere in the void
The solution is simple, you need to create a DB object before using it, e.g.:
void MainWindow::on_pushButton_clicked()
{
conn = new DB();
QString driverfromdb = conn->getDriver();
qDebug() << driverfromdb;
}
or just let conn be a typical variable, not an pointer:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DB conn; // conn is created after creating MainWindow object
This way you have not to initialize anything and you will avoid crashes.
or:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), conn(new DB),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete conn;
delete ui;
}
The compiler wont let me instanciate fileprocessor *p; in mainwindow.ccp but I am able to in any other class.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
fileprocessor *p;
ui->setupUi(this);
QObject::connect(ui->Open, SIGNAL(clicked()),
this,SLOT(on_action_Open_triggered()));
}
MainWindow::~MainWindow()
{
delete ui;
}
-mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
I am given fileprocessor:undeclared identifier and p: undeclared identifier
#user4217633: Include fileprocessor.h in the mainwindow.cpp, not in mainwindow.h. There you need a forward declaration:
class fileprocessor; // Just this
You already have such a forward declaration, in fact:
namespace Ui {
class MainWindow;
}
I am currently learning Qt and I seem to have run into a problem.
In my practice project I have 2 classes: MainWindow and Dialog.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonDialog_clicked();
private:
Ui::MainWindow *ui;
Dialog *dialogInstance;
};
#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);
dialogInstance = new Dialog(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonDialog_clicked()
{
dialogInstance->show();
}
Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
My goal is to input a value using the Dialog window, then have the value of that input shown on the MainWindow, I know how to pass variables around within the class using widgets, but I am not sure how to transfer variables between unrelated objects.
Any input would be of great help.
Try this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonDialog, SIGNAL(clicked()), this, SLOT(on_pushButtonDialog_clicked()));
dialogInstance = new Dialog(this);
}
// ...