I am new in Qt and i stacked in a task. I created a QGLWidget and i try to connect it with a QTablewidget. I want to take a variable from QTableWidget which i want to use in order to plot in the QGLWidget. The problem is that there are two classes, one for QGLWidget and one for ui (QDialog where QTableWidget is included) and i don't know how to take input from QTableWidget. Can i use signal and slot or i could have access in ui from QGLWidget and how can i do it? I would appreciate any thoughts.
You can do this without signal and slot. Use setter, you can set different types of variables and use it inside GLWidget:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QDebug>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void setValue(int i);
signals:
public slots:
private:
int member;
};
#endif // GLWIDGET_H
Cpp:
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget
(parent)
{
}
void GLWidget::setValue(int i)
{
member = i;
qDebug() << i;
}
Usage:
void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
{
GLWidget *wgt = new GLWidget;
wgt->setValue(index.data().toInt());
wgt->show();
}
Related
I have a MainWindow class which contain a QComboBox and a widget which is from another class. This second class contain a QCheckBox and a QComboBox. I want to use a signal to change the checkState of my QCheckBox and the string displayed in my QComboBox from my widget class when the string displayed in my QComboBox from my MainWindow has changed.
But I don't really understand which form my signal must have and how I can use it in my widget class.
MainWindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QComboBox>
#include "devices_left_widget.h"
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
signals:
public slots:
void carte_OK();
protected:
QComboBox* carte_type_combo_box;
devices_left_widget* left_widget;
};
#endif // MAINWINDOW_H
device_left_widget.h :
#ifndef DEVICE_LEFT_WIDGET_H
#define DEVICE_LEFT_WIDGET_H
#include <QWidget>
#include <QCheckBox>
#include <QComboBox>
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class device_left_widget : public QWidget {
Q_OBJECT
public:
explicit device_left_widget(QWidget* parent = nullptr);
signals:
public slots:
protected:
QGridLayout* main_grid_layout;
QCheckBox* device_checkbox;
QComboBox* device_type_combo_box;
};
#endif // DEVICES_LEFT_WIDGET_H
Let's call your widget's name container. We want to connect QComboBox's currentTextChanged(const QString &text) signal to the widget, so we create a slot that corresponds to the signal, let it be chosenTextChanged(const QString& text). We connect them inside MainWindow constructor:
connect(ui->comboBox, SIGNAL(currentTextChanged(const QString &)),
ui->container, SLOT(chosenTextChanged(const QString &)));
And inside your container class, define the slot as public:
public slots:
void chosenTextChanged(const QString &text) {
//change your QCheckBox's state and
//change your QComboBox's text
}
I have a QMainWindow Application which also includes an QStackedWidget.
The pages of the QstackedWidget are promoted to ui widgets for example heating_widget.ui
If I use a button slot on my QMainWindow I can use this to get my application to fullscreen:
void SmartHome::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
But how can I do this from a button which is in the heating_widget.cpp file?
Using:
void heating_widget::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
obviously doesn't work and throws this error at me:
cannot call member function 'void
QWidget::setWindowState(Qt::WindowStates)' without object
SmartHome::setWindowState(Qt::WindowFullScreen);
I know this has something to do with parent() but I can't get it to work.
Do you have any idea?
My smarthome.h file:
#ifndef SMARTHOME_H
#define SMARTHOME_H
#include <QTime>
#include <QMainWindow>
namespace Ui {
class SmartHome;
}
class SmartHome : public QMainWindow
{
Q_OBJECT
public:
explicit SmartHome(QWidget *parent = 0);
~SmartHome();
private slots:
void on_Info_Button_clicked();
void on_News_Button_clicked();
void on_Heating_clicked();
void timerslot();
void on_Config_clicked();
void on_About_clicked();
public slots:
void setFullscreen();
private:
Ui::SmartHome *ui;
QTimer* myTimer;
};
#endif // SMARTHOME_H
My heating_widget.h :
#ifndef HEATING_WIDGET_H
#define HEATING_WIDGET_H
#include "smarthome.h"
#include <QWidget>
namespace Ui {
class heating_widget;
class SmartHome;
}
class heating_widget : public QWidget
{
Q_OBJECT
public:
explicit heating_widget(QWidget *parent = 0);
~heating_widget();
private slots:
void on_fullscreen_on_clicked();
private:
Ui::heating_widget *ui;
};
#endif // HEATING_WIDGET_H
and my heating.widget.cpp:
#include "heating_widget.h"
#include "ui_heating_widget.h"
#include "smarthome.h"
#include "iostream"
heating_widget::heating_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::heating_widget)
{
ui->setupUi(this);
QObject::connect(ui->fullscreen_on, SIGNAL(clicked()), this , SLOT(SmartHome::setFullscreen()));
}
heating_widget::~heating_widget()
{
delete ui;
}
void heating_widget::on_fullscreen_on_clicked()
{
parentWidget()->setWindowState(Qt::WindowFullScreen);
std::cout<<"clicked"<<std::endl;
}
I would do it in the following way:
void heating_widget::on_fullscreen_on_clicked()
{
foreach(QWidget *widget, QApplication::topLevelWidgets()) {
if (auto mainWindow = qobject_cast<SmartHome *>(widget)) {
mainWindow->setWindowState(Qt::WindowFullScreen);
}
}
}
The idea is finding your main window among application top level widgets and change its state. This code can be called from anywhere in your application regardless of the windows hierarchy.
I am new to qt and I am trying to create a program where the MainWindow calls a QDialog to enter some data.
The problem is that the parent() at my QDialog does not have any access to the public methods of the MainWindow in our case the
void save_city(const City *city); //public method of MainWindow
The code is actually big so here is some of the code.Thanks.
mainwindow.h
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMainWindow>
#include <QTextStream>
#include <QVector>
#include <QDebug>
#include <QFile>
#include "dialog_add_city.h"
#include "street.h"
#include "city.h"
#include "map.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
//main Window constructor
explicit MainWindow(QWidget *parent = 0);
//main Window deconstructor
~MainWindow();
/*utility function to save the city
*the function is public so that also QDialogs
*data saves can */
//////////////
//here is the public function
void save_city(const City *city);
private slots:
private:
Map mainMap;
Ui::MainWindow *ui;
QGraphicsView view;
QGraphicsScene scene;
};
dialog_add_city.h
#ifndef DIALOG_ADD_CITY_H
#define DIALOG_ADD_CITY_H
#include <QDialog>
#include "mainwindow.h"
#include "City.h"
namespace Ui {
class Dialog_Add_City;
}
class Dialog_Add_City : public QDialog
{
Q_OBJECT
public:
explicit Dialog_Add_City(QWidget *parent = 0);
~Dialog_Add_City();
private slots:
//Add New City Button clicked-Adds an new city to our city_locations.txt
void on_PushButton_Add_New_City_clicked();
private:
Ui::Dialog_Add_City *ui;
};
#endif // DIALOG_ADD_CITY_H
dialog_add_city.cpp
#include "dialog_add_city.h"
#include "ui_dialog_add_city.h"
Dialog_Add_City::Dialog_Add_City(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_Add_City)
{
ui->setupUi(this);
}
Dialog_Add_City::~Dialog_Add_City()
{
delete ui;
}
void Dialog_Add_City::on_PushButton_Add_New_City_clicked()
{
City *city=new City(ui->lineEdit_cityName->text(),
ui->lineEdit_X_Ko->text().toDouble(),
ui->lineEdit_Y_Ko->text().toDouble());
qDebug() << ui->lineEdit_cityName->text()
<< ui->lineEdit_X_Ko->text()
<< ui->lineEdit_Y_Ko->text();
/////////////////////////////
//HERE IS THE PROBLEM
parent()->save_city(city);
}
Any other suggestions are welcomed!
The Problem is that parent() will return a pointer the parent object as QObject.
QObject Documentation
As dreschrjm pointed out you could try to cast the object via
MyWidget *myWidget = qobject_cast<MyWidget *>(obj);
from The Meta-Object System
Suggestion: Use Qt's signal/slot mechanism to avoid a backward reference to the parent.
Declare save_city as signal in Dialog_Add_City.
Declare save_city in MainWindow as slot
Connect both where the dialog is created:
e.g.
void MainWindow::show_add_city_dialog()
{
Dialog_Add_city dialog;
connect(&dialog, &Dialog_Add_city::save_city, this, &MainWindow::save_city);
dialog.exec();
}
Replace parent()->save_city(city); in Dialog_Add_City with emit save_city(city);
Ensure that the new city object doesn't leak, I think a better design would be to create the City object elsewhere not in the dialog.
so
emit save_city(ui->lineEdit_cityName->text(),
ui->lineEdit_X_Ko->text().toDouble(),
ui->lineEdit_Y_Ko->text().toDouble());
and the slot
void MainWindow::save_city(const QString &cityName, double x, double y)
{
City *city=new City(cityName, x, y);
// do some interresting things with city.
}
While doing some threading tutorials I got carried away and decided to make a gui which will show me the effect of multiple threads writing to one variable and using mutex.
The app uses mainwindow.ui menu to create a new instance of the threaddialog class every time which then runs it's own thread, displaying it's count loop on labels. Before I get onto doing the loop and having a mutex 'toggle', I am trying to connect the count update between the mainwindow and threaddialog so mainwindow can show the global count updating.
I can't get the connect() right, I am trying to pass it a pointer to the new threaddialog I just made before it, as that will be signalling the count, and the signal itself. Then for slot I use the this pointer to send the address of MainWindow, as that is where the slot is located, and the slot name itself.
As it stands, the connect() line gives me this error for both signal and slot parameters.
C:\Users\btank\Documents\Qt Projects\QThreadClasses\mainwindow.cpp:46: error: C3867: 'ThreadDialog::gCountUpdate': non-standard syntax; use '&' to create a pointer to member
I have read the whole page on Qt signals and slots official docs to try and understand what I'm doing wrong but no luck and need help. I don't believe I'm doing anything wrong regarding sending those pointers to connect().
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGridLayout>
#include <threaddialog.h>
#include <QLinkedList>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
QGridLayout *layout = new QGridLayout();
void NewThread();
~MainWindow();
private slots:
void on_actionNew_Thread_triggered();
void on_actionDelete_Thread_triggered();
public slots:
void setGCount(int gCount);
private:
QLinkedList<ThreadDialog *> list;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <threaddialog.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Clear memory list before using
list.clear();
qDebug() << list.count();
// Set layout in widget
QWidget *window = new QWidget();
window->setLayout(layout);
// Add widget to main window central widget
setCentralWidget(window);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_Thread_triggered()
{
NewThread();
}
void MainWindow::NewThread()
{
ThreadDialog *newThread = new ThreadDialog(list.count());
qDebug() << list.count();
connect(newThread, newThread->gCountUpdate, this, this->setGCount);
newThread->run();
list << newThread;
layout->addWidget(newThread);
}
void MainWindow::on_actionDelete_Thread_triggered()
{
layout->removeWidget(list.last());
delete list.last();
list.removeLast();
qDebug() << list.count();
}
void MainWindow::setGCount(int gcount)
{
ui->lblGCount->setText(QString::number(gcount));
}
threaddialog.h
#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QDialog>
#include <QThread>
namespace Ui {
class ThreadDialog;
}
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
explicit ThreadDialog(int count, QWidget *parent = 0);
void run();
~ThreadDialog();
signals:
void gCountUpdate(int uCount);
private:
Ui::ThreadDialog *ui;
};
#endif // THREADDIALOG_H
threaddialog.cpp
#include "threaddialog.h"
#include "ui_threaddialog.h"
#include "mainwindow.h"
ThreadDialog::ThreadDialog(int count, QWidget *parent) :
QDialog(parent),
ui(new Ui::ThreadDialog)
{
ui->setupUi(this);
// Setup UI
ui->lblTCount->setText(QString("Thread %1").arg(count));
}
ThreadDialog::~ThreadDialog()
{
delete ui;
}
void ThreadDialog::run()
{
for(int i = 0; i < 100; i++)
{
qDebug() << (QString("Thread loop %1").arg(i));
ui->lblTNum->setText(QString("Thread %1").arg(i));
emit this->gCountUpdate(i);
QThread::sleep(100);
}
}
This is how it should look like:
connect(newThread, &ThreadDialog::gCountUpdate
this, &MainWindow::setGCount);
You need to get a pointer to the method, not call the method.
I would like to map a radio buttons to a QDataWidgetMapper as per this forum thread. I am not sure though how to hook up the delegate mentioned. Could anyone explain this?
I already have .ui files that contain the radio buttons, among QLineEdits etc. Those are already hooked via a QDataWidgetMapper to a QSqlTableModel. Can I use the solution mentioned in the url above to add the existing radio buttons in the .ui field to the button group, and get the state into the DB?
My radio buttons are actually binary selections, so storing boolean values in the db would be sufficient. I do not want to use a checkbox because radio buttons make the selection more easily comprehensible to the user, in this case.
So far I have added includes to the example .h file
#include <QWidget>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QAbstractButton>
and created the .cpp file.
#include "buttongroup.h"
ButtonGroup::ButtonGroup(QWidget *parent) :
QWidget(parent)
{
m_buttonGroup=new QButtonGroup(this);
m_layout=new QVBoxLayout(this);
connect(m_buttonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));
setLayout(m_layout);
}
int ButtonGroup::getCheckedId()
{
int id=m_buttonGroup->id(m_buttonGroup->checkedButton());
return id;
}
void ButtonGroup::checkId(int id)
{
m_buttonGroup->button(id)->setChecked(true);
}
void ButtonGroup::addButton(QAbstractButton *button, int id)
{
m_buttonGroup->addButton(button,id);
m_layout->addWidget(button);
//connect(button,SIGNAL(clicked()),this,SIGNAL(buttonClicked()));
}
The delegate needs to be connected to the mapper.
I have created a sample project that works at http://scanrobot.fi/wp-content/uploads/2015/06/qradiobutton-qsqltablemodel.zip
Also at: https://github.com/savolai/qt_qradiobutton_qdatawidgetmapper_example
Here is the essence of what I needed to understand in practice:
QDataWidgetMapper* mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->toLast();
QRadioButton *b1=new QRadioButton("a",this);
QRadioButton *b2=new QRadioButton("b",this);
RadioButtonDelegate *delegate=new RadioButtonDelegate(this);
ButtonGroup *group=new ButtonGroup(this);
mapper->addMapping(group,model->fieldIndex("radio"));
// set the delegate as the item delegate of mapper
mapper->setItemDelegate(delegate);
// connect value change in group to delegate so it can send appropriate signals
connect(group,SIGNAL(buttonClicked(int)),delegate,SLOT(commitMyData()));
group->addButton(b1,1);
group->addButton(b2,0);
Source for buttongroup.cpp:
#include "buttongroup.h"
ButtonGroup::ButtonGroup(QWidget *parent) :
QWidget(parent)
{
m_buttonGroup=new QButtonGroup(this);
connect(m_buttonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));
}
int ButtonGroup::getCheckedId()
{
int id=m_buttonGroup->id(m_buttonGroup->checkedButton());
return id;
}
void ButtonGroup::checkId(int id)
{
m_buttonGroup->button(id)->setChecked(true);
}
void ButtonGroup::addButton(QAbstractButton *button, int id)
{
m_buttonGroup->addButton(button,id);
}
buttongroup.h (nothing very new here compared to the original url):
#ifndef BUTTONGROUP_H
#define BUTTONGROUP_H
#include <QWidget>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QAbstractButton>
#include <QRadioButton>
class ButtonGroup : public QWidget
{
Q_OBJECT
public:
explicit ButtonGroup(QWidget *parent = 0);
Q_PROPERTY(int checkedId READ getCheckedId WRITE checkId USER true)
int getCheckedId();
void checkId(int id);
void addButton(QAbstractButton *button, int id);
signals:
void buttonClicked(int);
private:
QButtonGroup *m_buttonGroup;
QVBoxLayout *m_layout;
};
#endif // BUTTONGROUP_H
radiobuttondelegate.h (nothing very new here either):
#ifndef RADIOBUTTONDELEGATE_H
#define RADIOBUTTONDELEGATE_H
#include <QItemDelegate>
class RadioButtonDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit RadioButtonDelegate(QObject *parent = 0);
signals:
public slots:
void commitMyData();
};
#endif // RADIOBUTTONDELEGATE_H
radiobuttondelegate.cpp (or here):
#include "radiobuttondelegate.h"
#include <QDebug>
RadioButtonDelegate::RadioButtonDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
void RadioButtonDelegate::commitMyData() {
QWidget *obj = qobject_cast<QWidget*>(sender());
emit commitData(obj);
emit closeEditor(obj);
qDebug() << "commitMyData";
}