Qt get combobox currentIndex from another class(out of ui class) - c++

I want to get the currentIndex of combobox in the main Widget, then send this value to another class. I tried to make a function to return the currentIndex, however, I always get '0'. Could someone explain me why, or give me an example? Plus, I think it's because I create a new object in another class, but how can I get the actived Widget's pointer? Here are the codes:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui { class Widget; }
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
int comboboxText();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui; };
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "database.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
int Widget::comboboxText(){
return ui->comboBox->currentIndex();
}
void Widget::on_pushButton_clicked()
{
qDebug() << "from Widget: " << comboboxText();
Database database;
database.getIndex();
}
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QDebug>
class Database : public QObject
{
Q_OBJECT
public:
explicit Database(QObject *parent = 0);
void getIndex();
signals:
public slots:
};
#endif // DATABASE_H
database.cpp
#include "database.h"
#include <widget.h>
Database::Database(QObject *parent) : QObject(parent)
{
}
void Database::getIndex(){
Widget a;
qDebug()<< "from database: " << a.comboboxText();
}

Related

How can I expand and collapse part of my form with QPropertyAnimation

I want is to show and collapse the groupBox by clicking on the button; so far it works, but i think its not the correct way to do.
The animation looks very bad when the form is expanded and when it is contracted it is a little more normal.
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPropertyAnimation>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QPropertyAnimation *animation;
bool expand=false;
int height;
};
#endif // WIDGET_H
#include "widget.h"
#include "./ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pushButton->setText("expand");
height=this->size().height();
ui->groupBox->setVisible(false);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
if(!expand){
animation=new QPropertyAnimation(this,"size");
animation->setDuration(250);
animation->setEndValue(QSize(this->width(),height));
animation->setEasingCurve(QEasingCurve::InQuad);
animation->start();
ui->groupBox->setVisible(true);
expand=true;
ui->pushButton->setText("Shrink");
}else{
ui->groupBox->setVisible(false);
animation=new QPropertyAnimation(this,"size");
animation->setDuration(250);
animation->setEndValue(QSize(this->width(),(height-ui->groupBox->size().height())));
animation->setEasingCurve(QEasingCurve::InQuad);
animation->start();
expand=false;
ui->pushButton->setText("Expand");
}
}

Example SLOT/SIGNAL between two object QT

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.

Qt slot method (which is another class) not firing when emiting the signal

I know this question asked many times but still confused and cannot find a solution.
I have a MainWindow and a class.
In my class I have a signal which I emit in the method call.
Problem: Slot method is not firing.
Here is my code.
BWorker.h
#pragma once
#include <QObject>
class BWorker : public QObject
{
Q_OBJECT
public:
BWorker(QObject *parent);
~BWorker();
void doSomething();
signals:
void signalSomething();
};
BWorker.cpp
#include "BWorker.h"
BWorker::BWorker(QObject *parent)
: QObject(parent)
{
}
BWorker::~BWorker()
{
}
void BWorker::doSomething()
{
emit signalSomething();
}
QtGuiApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
void workDone();
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "BWorker.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
BWorker bworker(this);
connect(&bworker, SIGNAL(bworker.signalSomething), this, SLOT(workDone()));
bworker.doSomething();
}
void QtGuiApplication1::workDone() {
}
Your problem is here:
SIGNAL(bworker.signalSomething)
This is not valid and you should see a message in terminal saying that there is no such signal. The correct syntax for your case would be:
SIGNAL(signalSomething())
Please post your code by copy-pasting it. Do not edit the code: The problem could be emerged from where you edited.
The code of mine worked fine.
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();
public slots:
void workDone();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "worker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Worker worker(this);
connect(&worker, SIGNAL(signalSomething()), this, SLOT(workDone()));
//connect(&worker, &Worker::signalSomething, this, &MainWindow::workDone);
worker.doSomething();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::workDone()
{
qDebug() << "Done.";
}
worker.h:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void doSomething();
signals:
void signalSomething();
public slots:
};
#endif // WORKER_H
worker.cpp:
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::doSomething()
{
emit signalSomething();
}
Application Output:
Debugging starts
Done.
Debugging has finished
Printed out "Done." means the codes are working.

Application crashing after calling a method

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;
}

How to access ui components from another class?

I am trying to call a method called display_txt() from my_class. This method is supposed to change some text in a label. But the method doesn't make any changes to the ui. I know the method runs due to the qDebug(). No errors occur, but the label just doesn't change to "text changed". display_txt() works fine and changes the label when called from the main widget class. There is a similar question in the forums but I couldn't use those solutions. I make an object when the button btn is pressed.
Here is the code:
widget.h
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void display_txt();
private slots:
void on_btn_clicked();
private:
Ui::Widget *ui;
};
my_class.h
#include <QWidget>
#include "widget.h"
class my_class : public QWidget
{
Q_OBJECT
public:
explicit my_class(QWidget *parent = 0);
void test(Ui::Widget ui1);
signals:
private:
Widget *wo = new Widget;
};
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "my_class.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//display_txt();
qDebug() << "Widget object created";
}
void Widget::display_txt(){
ui->lbl->setText("text changes!!!!!");
qDebug() << "display_txt() method finished";
}
void Widget::on_btn_clicked()
{
my_class mc;
}
my_class.cpp
#include "my_class.h"
#include "QDebug"
my_class::my_class(QWidget *parent) : QWidget(parent)
{
qDebug() << "myclass ran";
wo->display_txt();
}