Qt using parametre of the signal [duplicate] - qt

This question already has answers here:
Get index of QPushButton on 2D array QPushButton
(2 answers)
Closed 5 years ago.
I created a signal, which is emitted when the user input something(a number) in a qlineedit field, the signal is emitted with a parametre(the number that the user just type in the field). And i would like to use that parametre as a regular number(in a variable). Im trying to add that signal parametre to another number, and i had an error "s1 is not declared". Here is my class in the .h file and his implementation in the .cpp file
the.h file
class fenetre: public QWidget
{
Q_OBJECT
public:
fenetre();
public slots:
void calc();
void clearinput();
signals:
void thesecond(int s1);
private:
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QPushButton *button4;
QPushButton *button5;
QPushButton *result0;
QPushButton *clear;
QLineEdit *input1;
//QLineEdit *inputsqrt;
//QLineEdit *input2;
//QLineEdit *result;
//QLineEdit *square;
};
the.cpp file
QObject::connect(button1,SIGNAL(clicked()),this,SLOT(calc()));
//QObject::connect(button2,SIGNAL(clicked()),this,SLOT(calc()));
//QObject::connect(button3,SIGNAL(clicked()),this,SLOT(calc()));
//QObject::connect(button4,SIGNAL(clicked()),this,SLOT(calc()));
//QObject::connect(button5,SIGNAL(clicked()),this,SLOT(calc()));
//QObject::connect(clear,SIGNAL(clicked()),this,SLOT(clearinput()));
//QObject::connect(result0,SIGNAL(clicked()),this,SLOT(calc()));
QObject::connect(result0,SIGNAL(thesecond(int)),this,SLOT(calc()));
}
void fenetre::calc()
{
QString s=input1->text();
bool ok;
if(!input1->text().isEmpty())
{
int s1=s.toInt(&ok,10);
emit thesecond(s1);
input1->clear();
}
QObject* obj=sender();
if(obj==result0)
{
int s2=s.toInt(&ok,10);
int A=s2+thesecond(s1);
input1->clear();
QString c=QString::number(A);
input1->setText(c);
}
}

Your code doesn't make sense.
calc() doesn't pass a parameter. There is no s1 for you to use in calc(). There is one you declare in the first if statement, but since it is declared in the block, it is out of scope after the block. Then you are using the signal as a function with a return type, which is kind of odd, considering the signal itself is declared with a void return type. Signals in Qt can return values, but that's extremely rarely used, and it will have to actually return a value for that to work.
QPushButton doesn't have a thesecond(int s1) signal. The last connection statement fails. You don't even check whether it does.
If we assume that in the last connection you meant to say this instead of result0, then what you end up with is a button connecting to calc() which emits a signal, connected to... calc() again!
All in all, your code is conceptually entirely wrong, and you don't seem to know what you are doing. Also, you might want to learn a bit on proper coding conventions, because that code is atrocious.
I suggest you edit the question and carefully explain what you actually want to happen, because the way it is right now, your intent is a mystery. Judging by your other questions, it definitely looks like you are getting ahead of yourself, go back and do more learning before you try to use it. You will have a very bad time with programming if you don't have a clue what you are doing. There is a huge difference between making occasional mistakes and having no idea what you are doing, the former is the scope of this site, the second isn't.

Related

Can't figure out how or why this code solves an error. ESP32 arduino IDE with platform dot io

I am fairly new to coding and especially to C++ but usually with enough googling and breaking problems down to simpler blocks I can figure things out. This issue makes no sense to me though and the solution I just happened to come up with on accident makes even less sense to me.
I am writing a program for ESP32-S in vscode with platformio and broke this down to isolate what was causing the error and found this issue with class/object declaration:
This code will compile but I get a link error twice that says
undefined reference to `point::point()'
#include <Arduino.h>
class point {
public:
point();
point(uint day){
this->_day = day;
}
uint _day;
};
class channel {
public:
channel(String _color, byte _pin){
}
point _points[64];
};
channel red("red", 0);
void setup() {}
void loop() {}
Meanwhile this code with one seemingly unrelated change compiles and links without issue:
#include <Arduino.h>
class point {
public:
point();
point(uint day){
this->_day = day;
}
uint _day;
};
class channel {
public:
channel(){ // <--- Removing the arguments from channel constructor fixes it?
}
point _points[64];
};
channel red(); // <--- And here of course
void setup() {}
void loop() {}
I don't know why that fixes it, and I have a workaround for now if this is what I have to do, but I want to understand. Thanks.
You've declared a constructor point::point(), but not defined it (i.e. it has no body). This is not OK with the linker and that's what you're being told.
There are three ways to fix this.
Don't declare the constructor (compiler generates a default constructor which may or may not initialize the member _day to 0, depending on compiler and platform). Note that you also have the interesting alternative of deleting the default constructor.
Declare and define it to do whatever you consider appropriate.
Give your constructor point::point(uint day) a default argument value, e.g.: point::point(uint day = 0).
Side note on C style arrays of C++ objects like here:
point _points[64];
This is a dangerous combination, unless you know exactly what you're doing. See the C++ FAQ

Is there a signal/slot connection in QT to change colors based on values changing

I am trying to change some formatting in QT based on some values changing. I am using some epicsQT widgets which read some values off instruments (humidity is one example). I want the color of a button to change (and flash, if possible) to change if the value exceeds some limit (let's say 85 percent for humidity).
Is there a signal/slot combination to use? I've tried the update(), and tried to model the painterEvent that's covered in the analogClock example. But I don't think this is an "event." Aren't events different than signals?
For testing, I've been trying with just a simple spinBox, and a qanalogSlider to try to get the MainWindow to change color. I just want the color to change (and possibly flash) if the value goes over something arbitrary (say greater than 5).
You can create your own signals and slots so you can do something like:
private slots:
void SlotThatFormat();
signals:
void mySignal();
and then use emit(mySignal()); and a connect(A, SIGNAL(mySignal()),B, SLOT(SlotThatFormat()));
As you can create them, you can decide what type of arguments they use and what they do. So if you, for example want to use a bool as an argument and change the color you could do something like:
private slots:
void mySlot(bool value);
signals:
void mySignal(bool value); // remember that both slot and signal need to use the same type of argument
private:
bool boolVar;
Then in cpp constructor or where you want:
connect(A, SIGNAL(mySignal(bool)), B, SLOT(mySlot(bool)));
and when you want to activate it you need to emit the signal like:
emit(mySignal(boolVar);
Finally in cpp you can do what you want with your slot which is something like a function:
void YourWindow::mySlot(bool value)
{
//Here code that changes what you need
}

How to pass argument through signal and slots?

My GUI consists of a LineEdit and a PushButton. When the PushButton is clicked, the slot clicked() is called. I want to setup a signal-slot relationship between clicked() as the signal and doSomething() as the slot. The issue is that doSomething() does not have access to the UI and doSomething() relies on the text of LineEdit to work.
I see two solutions:
Grant doSomething() access to the UI.
I want to be able to pass a QString to doSomething() as an argument. clicked() does not accept any arguments. Qsignalmapper might be what I'm looking for. However, it seems like the mapper only passes arguments based on what the signal is. The arguments therefore need to be setup in advance and it seems like it does not help me here.
How would I do solution 2?
Assuming a C++ solution, you might consider using an intermediate signal like the following for #2:
connect(thisWidget, SIGNAL(clicked()),
thisWidget, SLOT(slotClickHandler()));
then:
void slotClickHandler()
{
QString s = "my variable string here";
emit sigDoSomething(s);
}
and them:
void slotDoSomething(const QString &s)
{
// Do something interesting with s here
}
Note that this snippet assumes you've defined a new signal sigDoSomething(const QString &) and a private slot void slotDoSomething(const QString &).

Passing signals through hierarchies

I am having some difficulty fully grasping how signals and slots are used in Qt. I am sure it is really basic but I'm just no getting it today.
I have a set of widgets a bit like this:
MainWindow
-->StackedWidget
-->ChildForms
Now the idea is that there are some actions on the Child widgets that will cause the stacked widget to display a different page.
So if I understand it properly I thought the way to connect signals and slots is to use the connect() at the scope that knows about the objects but what I have managed to get working doesn't do it this way. At the moment in my child form I use parentWidget() to access the slot of the StackedWidget but I am not very happy with really because it is giving the child information about the parent which it shouldn't have:
void TaskSelectionForm::setButtonMappings()
{
// Set up a mapping between the buttons and the pages
QSignalMapper *mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(int)), parentWidget(), SLOT(setCurrentIndex(int)));
mapper->setMapping(ui->utilitiesButton, 2); // Value of the index
connect(ui->utilitiesButton, SIGNAL(clicked()), mapper, SLOT(map()));
}
But I am not really sure how I should do this and connect it up. Do I need to have signals at each level and emit through the tree?
A Bit of Signal-Slot Theory
The signal-slot connections are oblivious to parent-child relationships between QObjects, and any such relationship doesn't matter. You're free to connect objects to their children, to their siblings, to their parents, or even to QObjects that are in a separate hierarchy, or to lone QObjects that have neither parents nor children. It doesn't matter.
A signal-slot connection connects a signal on a particular instance of QObject to slot on another instance of QObject. To use the connect method, you need the pointers to the instance of sender QObject and the instance of receiver QObject. You then use the static QObject::connect(sender, SIGNAL(...), receiver, SLOT(...)). Those connections have nothing to do with any hierarchy there is between the sender and receiver.
You can also connect a signal to a signal, to forward it -- for example from a private UI element to a signal that's part of the API of the class. You cannot connect a slot to a slot, because it'd incur a bit of runtime overhead for a rarely-used case. The overhead would be an extra bool member in QObjectPrivate, plus a failed if (bool) test. If you want to forward slots to slots, there are at least two ways to do it:
Emit a signal in the source slot and connect that signal to the destination slot.
Obtain a list of all signals connected to the source slot, iterate on it and connect them to to the target slot. There's no easy way to maintain such connections when further signals are connected or disconnected from the source slot. Unfortunately, QObject only has a connectNotify(const char*) protected function, but not a signal -- so you can't hook up to it unless you would modify src/corelib/kernel/qobject[.cpp,_p.h,.h] to emit such a signal. If you truly need it, just modify the Qt source, you have access it for a reason, after all. Hacking the vtable without modifying Qt is possible, but discouraged for obvious reasons.
The Answer
Below is a self contained example that shows how to do what you want. Turns out I have answers to quite a few questions from my various experiments I've done in Qt in the past. I'm a packrat when it comes to test code. It's all SSCCE to boot :)
// https://github.com/KubaO/stackoverflown/tree/master/questions/signal-slot-hierarchy-10783656
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class Window : public QWidget
{
QSignalMapper m_mapper;
QStackedLayout m_stack{this};
QWidget m_page1, m_page2;
QHBoxLayout m_layout1{&m_page1}, m_layout2{&m_page2};
QLabel m_label1{"Page 1"}, m_label2{"Page 2"};
QPushButton m_button1{"Show Page 2"}, m_button2{"Show Page 1"};
public:
Window(QWidget * parent = {}) : QWidget(parent) {
// the mapper tells the stack which page to switch to
connect(&m_mapper, SIGNAL(mapped(int)), &m_stack, SLOT(setCurrentIndex(int)));
// Page 1
m_layout1.addWidget(&m_label1);
m_layout1.addWidget(&m_button1);
// tell the mapper to map signals coming from this button to integer 1 (index of page 2)
m_mapper.setMapping(&m_button1, 1);
// when the button is clicked, the mapper will do its mapping and emit the mapped() signal
connect(&m_button1, SIGNAL(clicked()), &m_mapper, SLOT(map()));
m_stack.addWidget(&m_page1);
// Page 2
m_layout2.addWidget(&m_label2);
m_layout2.addWidget(&m_button2);
// tell the mapper to map signals coming from this button to integer 0 (index of page 1)
m_mapper.setMapping(&m_button2, 0);
connect(&m_button2, SIGNAL(clicked()), &m_mapper, SLOT(map()));
m_stack.addWidget(&m_page2);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
Connect(stackedwidget->currentactivewidget,SIGNAL(OnAction()),this,SLOT(PrivateSlot()));
PrivateSlot() is a slot declared privately. So in this function, you can add your code to change the page of stackedwidget corresponding to the action produced by currentactivewidget.
Again if you really want to pass the signal up the heirarchy, emit a publicsignal() at the end of private slot function.
Connect(this,SIGNAL(publicsignal()),Parentwidgetofstackedwidget(here mainwindow),SLOT(mainwindow_slot()));

Connecting multiple signals to a single slot in Qt

I'm trying to keep track of the textChanged() signal on for handful of QTextEdits. I want to do the same thing regardless of the text edit emitting the signal: uncheck its associated checkbox in a QListWidget if it becomes empty and leave it checked otherwise. The function I have so for is as follows:
void MainWindow::changed()
{
QString tempStr = ui->hNMRedit->toPlainText();
if(tempStr != "")
{
ui->checkList->item(0)->setCheckState(Qt::Checked);
}
else
{
ui->checkList->item(0)->setCheckState(Qt::Unchecked);
}
}
With the current approach, I would have to make a function like this for every QTextEdit; each function containing virtually identical code. If I stored each of the text edits in an array (so I could find their associated index in the QListWidget), would it be possible for me to have a slot like this?
void MainWindow::changed(QWidget *sender) // for whichever text edit emits the
// textChanged() signal
{
QString tempStr = sender->toPlainText();
if(tempStr != "")
{
// I would potentially use some sort of indexOf(sender) function on the array I
// mentioned earlier here... a little new to Qt, sorry
ui->checkList->item(array.indexOf(sender))->setCheckState(Qt::Checked);
}
else
{
// same as above...
ui->checkList->item(array.indexOf(sender))->setCheckState(Qt::Unchecked);
}
}
Is this possible or should I just create a separate slot for every text edit?
Please let me know if any further clarification is needed!
Lastly, I feel like the only meaningful difference between QLineEdits and QTextEdits is the default size. In favor of keeping things consistent, should I just use one of these objects throughout my UI?
Thanks!!!
I think you are missing the point of slots and signals. How are you creating the connections?
Are you trying to check a box when any of the text boxes change? If so use a QSignalMapper to map the textChanged() signals to send a value of true and connect that to the QCheckBox setChecked(bool) slot.
If that is too complicated subclass QCheckBox and create a set of functions checkBox() uncheckBox() so you can toggle states without a variable. Then connect the QTextEdit textChanged() to your subclass checkBox()
If this is not what you are looking for, at least subclass QTextEditto take in a QCheckBox that it can change when the text changes instead of duplicating code for every QTextEdit
All you need is a hash of QAbstractButton*, keyed by QTextEdit*. In the slot, you look up the sender() in the hash, if found you've got the button you need. This is precisely what is done by the QSignalMapper: you can map from a sender QWidget* to your button QWidget*. Use qobject_cast to cast to QAbstractButton*.

Resources