This question already has answers here:
Passing an argument to a slot
(6 answers)
Closed 6 years ago.
I'm new in Qt... I'm creating N buttons based on a JSON file. And I need to run a function/slot when a button is clicked and I need to know which buttton was pressed.
I tried:
QObject::connect(button, &QToolButton::clicked, this, &base::show_brands(json, type));
show_brands(json, type) is a function/slot...
but I can't send args like this...
How can I sent args to my function/slot? Or how can I run a function when a button is clicked?
You want to use QSignalMapper;
http://doc.qt.io/qt-5/qsignalmapper.html#details
This lets you send signals from a collection of objects to a single method which can identify the source object.
Related
This question already has answers here:
Convert Windows Message IDs to Text
(3 answers)
Closed 4 years ago.
I can get events HexCode by onMessage event of applicationEvents component.
By this code:
MessagesList.Items.Add(IntToStr(MSG.message));
But I want get MessageText and add it into ListBox. For example when user pressed LeftMouseButton add "WM_LBUTTONDOWN" in ListBox.
I want List and Log All of Events handled in form.(onclick,onmousedown,onmouseup,onmousemove,....)
You have to construct your own lookup table for this. There is no built-in facility that will convert a message ID to a string representing the message's name.
This question already has answers here:
Increase check box size not its text using QCheckbox?
(3 answers)
Closed 4 years ago.
I have created a QCheckbox in QT Creator called 'override'.
I want to change the size of the actual checkbox, not the font of the text associated with it.
Is there a way of doing this?
Thanks for the help - I used this:
eyeChk = new QCheckBox("Eyes:");
_eyeChk->setStyleSheet("QCheckBox::indicator { width:150px; height: 150px;} QCheckBox::indicator::checked {image: url(/home/jvdglind/Downloads/280px-PNG_transparency_demonstration_2.png);}");
And just found sound decent default checkbox images.
I have two forms one is trainee_view.ui
and other is enter_new_trainee.ui
so for that i have trainee_view.cpp,trainee_view.h to see the list of Trainee in DB
and enter_new_trainee.cpp,enter_new_trainee.h to enter new trainee details
now in trainee_view.ui i have a push button "ADD Trainee"
so if i click this button it will go to "enter_new_trainee.ui"
void trainee_view::on_pushButton_2_clicked()
{
newtrainee=new enter_new_trainee(this);
newtrainee->setWindowFlags(Qt::Window);
newtrainee->show();
// connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
}
so by using connect() i am trying to refresh the trainee_view after entering the new trainee details. so how can i emmit the signal from
2nd form to 1st form such that i call refresh_form() method in 1st form .
I tried to use destroyed() signal on newtrainee but could not refresh my trainee_view form.
To be MOre simple . i just want to get an object is destroyed or not so if destroyed i can call refresh() method to load back the changes done on widget
for that i opted connect() method so how should i call that. becoz if i call
connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
there is no effect i.e nothing is loading into the view.
am newbie to qt so pls try to help me.
Thank YOu.
I'm not sure if I correctly understand your app, but I think you misunderstand the concept of Signals and Slots. Look here for some examples. In some simplification you can look at signal and slots this way: connect() command is a place which will not do anything - it just stay and keep listening for a signal. So you should place it in trainee_view.cpp. That's the first part and I see you did it correct, or almost correct. But you need also something that will send the signal, and this is exactly what emit() command do - it should be placed in enter_new_trainee.cpp just after description of generation new entry. For example, let assume user input new entry in LineEdit in UI:
[...]
QString newEntry = ui->LineEdit->text(); //Save entry to variable
emit(newEntry); //Emit it to signal slot
[...]
This question already has answers here:
Passing an argument to a slot
(6 answers)
Closed 6 years ago.
I started working on a project that requires the using of the TableView. My table has 3 columns and the last column has a comboBox. Using the Delegate I managed to set the comboBox and to retrieve a signal when the index status of the comboBox changes. The problem is I can not identify from witch comboBox the signal is emited from.
If I signal to the mainWindow the QString of the comboBox this seems to be very bad. I was thinking at a solution to insert into the comboBox from each line the index of the row. Something like row + name.
I initiate the connection using the advice from another post, such like:
signals:
void boxDataChanged(const int & str);
In create editor:
QComboBox * editor = new QComboBox(parent);
editor->addItem("This");
editor->addItem("is");
editor->addItem("nice");
connect(editor, SIGNAL(currentIndexChanged(int)), this, SIGNAL(boxDataChanged(int)));
return editor;
And called like:
connect(mydelegate, &Delegate::boxDataChanged, [=](const int & str)
{
qDebug() << str;
});
This is working nice but I also need to know from witch row this is comming.
The problem is I can not identify from witch comboBox the signal is
emited from.
You can use QObject::sender to get the sender of the signal.
It will return a QObject that you can cast into the desired type.
This question already has an answer here:
Qt, no such slot
(1 answer)
Closed 7 years ago.
I'm creating my first GUI App in QT, I'm newbie at it.
This application is supposed to count calories for me with little more options than webapps offer me.
In my program I got 4 lineedits :
lineEdit_Carbs
lineEdit_Fats
lineEdit_Protein
lineEdit_Calories
I want to do like "Realtime counter", if user provides value to any of the 3 cells, carbs,fats,proteins it shows how many calories is it already.
I tried to do this
connect(ui->lineEdit_Carbs,SIGNAL(textChanged(QString)),ui->lineEdit_Calories,SLOT(setText(CALORIE_COUNT(ui->lineEdit_Carbs->text(),ui->lineEdit_Fats->text(),ui->lineEdit_Proteins->text()))))
CALORIE_COUNT function takes 3 arguments, 3 QStrings, returns calculated QString containing the calories.
I would have to do this connection 3 times, for each lineEdit containing macronutrient.
But this seem to not work because
QObject::connect: No such slot QLineEdit::setText(CALORIE_COUNT(ui->lineEdit_Carbs->text(),ui->lineEdit_Fats->text(),ui->lineEdit_Proteins->text())) in ..\CalcProto\mainwindow.cpp:22
It says there is no such slot.
How should I create slot to make it work?
connect(ui->lineEdit_Carbs,
SIGNAL(textChanged(QString)),
this,
SLOT(intermediateSlot()));//SLOT can ignore incoming arguments
private Q_SLOTS:
intermediateSlot()
{
QString calorie = CALORIE_COUNT(ui->lineEdit_Carbs->text(),
ui->lineEdit_Fats->text(),
ui->lineEdit_Proteins->text());
ui->lineEdit.setText(calorie);// you can emit a new signal here
// carring calorie and connect it to
// ui->lineEdit, which is more Qt-ish
}
You maybe can subclass QLineEdit and reimplement the setText() method, but I am not sure you can, since it not seems to be declared as virtual
What about connecting all the QLineEdit textChanged(QString) to the the same slot and do all the work in it ?
Something like
connect(ui->lineEdit_Carbs,SIGNAL(textChanged(QString)), UpdateCalc);
connect(ui->lineEdit_Calories,SIGNAL(textChanged(QString)), UpdateCalc);
connect(ui->lineEdit_Fats,SIGNAL(textChanged(QString)), UpdateCalc);
connect(ui->lineEdit_Proteins,SIGNAL(textChanged(QString)), UpdateCalc);
where the UpdateCalc slot do all the calculations ?
Eventually you can add a timer to add a little delay to the execution so if you insert something with more than one char, you don't fire the event every time.