Qt: slot not called on manual function call - qt

I have a checkbox with a slot called
void MainWindow::on_checkBoxPhaseUnwrap_stateChanged(int state)
This is automatically connected.
The constructor of MainWindow is like this
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
{
ui->setupUi(this);
setupWidgets();
}
In setupWidgets I set the value of the checkbox
ui->checkBoxPhaseUnwrap->setChecked(true);
However the slot is not called, and thus the data base in the background not updated.
Once the window is shown I can click on the checkbox and the slot is called as usual. Why is it not called when I call setChecked Note: In the gui designer the default value is already checked. Is this the reason?

In the gui designer the default value is already checked. Is this the reason?
Yes void QCheckBox::stateChanged(int state)
This signal is emitted whenever the checkbox's state changes, i.e., whenever the user checks or unchecks it.
That means it is not emitted if you try to change state from Qt::Checked to Qt::Checked.
Solution would be to emit signal yourself:
In setupWidgets add
emit ui->checkBoxPhaseUnwrap->stateChanged(Qt::Checked);

Related

How to connect a signal in Qdialog to a slot in QMainwindow

I work with QT Creator 4.9.1 and i've made a gui with the designer. I have a MainWindow with a stackedWidget and round about 60 pages, inside my mainwindow i have a button, with the onButton_clicked signal i open a dialog(show picture) to insert a number to set the page the user want to see inside the mainwindow.
My Problem is that the SIGNAL comes from the Dialog with the name on_pushButton_Enter_clicked and my SLOT is inside my mainwindow with the name setCurrentIndex(). I,ve read the post's: "How to Connect Signal from MainWindow to Slot in Dialog" and "Qt connect mainwindow and dialog using signal and slot".
But that doesn't help me because my dialog doesn't know about my mainwindow and i don't know how i can connect them.
Signal:
Dialognummer_eingeben.h
...
signals:
void enterButtonPressed();
...
void Dialognummer_eingeben::on_pushButton_Enter_clicked()
{
QString text = ui->lineEdit_Dialognummer->text();
ui->lineEdit_Dialognummer->setText("");
this->reject();
emit enterButtonPressed();
}
Slot:
Terminal::Terminal(QWidget *parent) : QMainWindow(parent), ui(new Ui::Terminal)
{
ui->setupUi(this);
QObject::connect(&dialog, SIGNAL(enterButtonPressed()), this, SLOT(setCurrentIndex()));
}
void Terminal::setCurrentIndex()
{
int num = dianr.getNum();
QString strNum = QString::number(num);
switch(num)
{
....
}
}
Edit: 1. Add signal and slot code
2. Make some changes inside the code
inside my mainwindow i have a button, with the onButton_clicked signal i open a dialog(show picture) to insert a number to set the page the user want to see inside the mainwindow.
You have to additionally add a signal in your dialog class which should be emitted once enter button was pressed, using on_pushButton_Enter_clicked as a function is not enough. Add a signal in the dialog class, like "enterButtonPressed()" and emit it in the function on_pushButton_Enter_clicked.
Inside the mainwindow (at some point where the dialog is created) add this line:
connect(dialog, SIGNAL(enterButtonPressed()), this, SLOT(SlotNameWhichShouldGetCalled()));
EDIT: Even if the above solution should work, a better solution came to my mind.
You generally should use the QDialog::accepted signal to connect to (see https://doc.qt.io/archives/qt-4.8/qdialog.html#accept).
Concrete steps:
In on_pushButton_Enter_clicked() at the bottom of the code add accept() instead of this-reject() (I assume you want the dialog to be closed succesful and not as rejected?)
Connect to the QDialog::accepted() signal by adding
QObject::connect(&dialog, SIGNAL(accepted()), this, SLOT(setCurrentIndex()));
Additionally ensure that you have no error in the connect(...) function. If the signal/slot is not found or is not matching you should see something in your application output in Qt Creator

how to disable parent window on open child in Qt?

I want to forbid any action in the main window, including its closure. while open child
I try:
// child window
Settings::Settings(QWidget *parent) :
QWidget(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
((QWidget*)parent)->setEnabled(false); // or parent->setEnabled(false);
...
makes segmentation fault on Settings->show();
and I try send signal from Settings constructor/destructor
to MainClass slot
void MainClass::Enable(bool enable)
{
qDebug() << "detect signal enable"; //
this->setEnable(enable);
}
but signal wasn't sent.
ofcourse i connect Settings to MainClass)).
signal emiting works in any other function of Settings.
For child windows it would be better to inherit from QDialog (not QWidget) because the first one is designed specially for dialogs. From Qt docs:
The QDialog class is the base class of dialog windows.
The method you need is QDialog::exec(). It opens your dialog as a modal window which will block the input for other application windows until it is closed.

Enabling a QPushButton when an item is selected in a QListView

I basically got a QPushButton and a QListView connected to a QStandardItemModel.
The QPushButton allows to delete the selected items in the QListView. I'm trying to get the QPushButton only enabled if at least one item is selected in the QListView, but I can't find the correct signal for that.
The right signal is QItemSelectionModel::selectionChanged. QItemSelectionModel object can be obtained using view->selectionModel().
clicked(QModleIndex) looks like it can be a decent starting point - if you setup a slot to receive that notification, you should be able to enable your button.
Using QtCreator/Designer:
void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
ui->pushButton->setEnabled(true);
}
Without QtCreator, the connection would break down into something like this (Qt4 style):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QListWidget* listWidget = new QListWidget();
// ... setup ui stuff, etc ...
connect(listWidget, SIGNAL(clicked(QModelIndex)),
this, SLOT(on_listWidget_clicked(QModelIndex));
}

error QPushButton in a dock widget is inaccessible

Newbie here, I put a dock widget in a main window, and there is a button in this dock widget panel, now I want to connect, this button with a function defined in the main window, it threw out an error, what should I do? Thanks
connect
(
perfectPanel_->btn_AAA,
SIGNAL(clicked()),
this,
SLOT(on_actionAAA_triggered()),
Qt::UniqueConnection
);
Error message is
$PWD/ui_perfectPanel.h: In constructor ‘xixi::xixi()’:
$PWD/ui_perfectPanel.h:71:18: error: ‘QPushButton* Ui_perfectPanel::btn_AAA’ is inaccessible
$PWD/xixi/xixi.cpp:51:25: error: within this context
Note that I have already managed to connect this with a toolbar button in the main window (xixi.cpp), it works great.
This happens because your dock class, perfectPanel, inherits privately from the generated ui class Ui::perfectPanel:
class perfectPanel : public QWidget, private Ui::perfectPanel
You could make that inheritance public, but shouldn't. Instead you should make the signal part of the perfectPanel class, and route the internal signal from the button to that external signal:
class perfectPanel ... {
...
signals:
void AAA_clicked();
};
perfectPanel::perfectPanel() {
setupUi(this);
connect(btn_AAA, SIGNAL(clicked()), this, SIGNAL(AAA_clicked()));
}
(And in case you would ask, yes, you can connect 2 signals together).
Then you simply connect the new signal inside your main window class:
connect(perfectPanel_,
SIGNAL(AAA_clicked()),
this,
SLOT(on_actionAAA_triggered()),
Qt::UniqueConnection
);

QT NOOB: Add action handler for multiple objects of same type

I have a simple QT application with 10 radio buttons with names radio_1 through radio_10. It is a ui called Selector, and is part of a class called TimeSelector
In my header file for this design, i have this:
//! [1]
class TimeSelector : public QWidget
{
Q_OBJECT
public:
TimeSelector(QWidget *parent = 0);
private slots:
//void on_inputSpinBox1_valueChanged(int value);
//void on_inputSpinBox2_valueChanged(int value);
private:
Ui::Selector ui;
};
//! [1]
the commented out void_on_inputSpinBox1_valueChanged(int value) is from the tutorial for the simple calculator. I know i can do:
void on_radio_1_valueChanged(int value);
but I would need 10 functions. I want to be able to make one function that works for everything, and lets me pass in maybe a name of the radio button that called it, or a reference to the radio button so i can work with it and determine who it was.
I am very new to QT but this seems like it should be very basic and doable, thanks.
You can create a unique slot and obtain the object that emitted the signal with the QObject::sender() method. The following code presents an example of such slot:
public slots:
void onRadioToggled(bool checked)
{
QRadioButton *radio = qobject_cast< QRadioButton* >(QObject::sender());
// radio is the object that emitted the triggered signal
// if the slot hasn't been triggered by a QRadioButton, radio would be NULL
if (radio) {
qDebug() << radio->objectName() << " is set to " << checked << ".";
}
}
Note that radio->objectName() will not give you a good result unless you define it explicitly somewhere.
Now, you can connect the toggled(bool checked) signal of every QRadioButton to the onRadioToggled slot. Note that QRadioButton does not have any valueChanged signal so your code cannot actually work.
connect(radio_1, SIGNAL(toggled(bool)), SLOT(onRadioToggled(bool)));
connect(radio_2, SIGNAL(toggled(bool)), SLOT(onRadioToggled(bool)));
...
connect(radio_10, SIGNAL(toggled(bool)), SLOT(onRadioToggled(bool)));
In the radiobutton case, add the buttons to a QButtonGroup.
Similar functionality offers QSignalMapper.
What you can do is to create your own radio button class that inherits from QRadioButton and create a signal. This signal can have all the parameters you want.
void CheckWithReference(YourRadioButtonClass* rb);
or
void CheckWithReference(QString RadioButtonName);
or anything you would like to have.
Then create a slot in your TimeSelector class with the same set of parameters you will connect to all signals.

Resources