Unsolved passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase
-
@MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:
why did it even emit when i add the argument to the built-infunction definition of QT
Who emits what? Noone stops you from changing a decoration of a function.
-
Dude there is already SLOT which is this
void Profile3::on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column){ // item1_AddingItemstoTreeWidget_3->setFlags(item1_AddingItemstoTreeWidget_3->flags() & ~Qt::NoItemFlags ); }
but when i pass this argument KItchen_Data &Kitchen to it , it doesn't work at all
void Profile3::on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,KItchen_Data &Kitchen){
NOW
i added this but it doesn't work
connect(ui->treeWidget_Kitchen, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),this, SLOT(Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen))) ;
i just want to make treeWidget ItemDoubleClick while i'm passing this Kitchen_Data &Kitchen argument to the parameters
This the new function i made that have the argument i want so i want when i double click items in the treeWidget executes the function below void Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);
Thanks in advance
-
@MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:
connect(ui->treeWidget_Kitchen, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),this, SLOT(Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen))) ;
This simply can't work! The signal you're connecting does not provide Kitchen_Data &Kitchen, right?
Connect a lambda function as slot instead and call your on_treeWidget_Kitchen_itemDoubleClicked slot in this lambda passing Kitchen_Data &Kitchen. -
@MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:
Dude there is already SLOT which is this
But QTreeWidget have no corresponding signal (as I already told you).
-
I Added the Argument Kitchen_Data &Kitchen to the SIGNAL it didn't work
connect( ui->treeWidget_Kitchen, SIGNAL(itemDoubleClicked(QTreeWidgetItem* item, int column ,Kitchen_Data &Kitchen)),this, SLOT(Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen))) ;
i also used Lambda function following this New_Signal_Slot_Syntax did n't work also
QTreeWidget * treee =ui->treeWidget_Kitchen ; connect( treee, treee::itemDoubleClicked ,this, SLOT(Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen))) ;
and it there is an error but i'm not sure if used it in the right way
D:\New folder\Hospitaal\profile3.cpp:20: error: 'treee' is not a class, namespace, or enumeration ..\Hospitaal\profile3.cpp:20:22: error: 'treee' is not a class, namespace, or enumeration connect( treee, treee::itemDoubleClicked ,this, SLOT(Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen))) ; ^~~~~
Thanks in advnace
-
@MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:
i also used Lambda function following this New_Signal_Slot_Syntax did n't work also
I don't see any lambda in your code.
It would look like this:connect(ui->treeWidget_Kitchen, &QTreeWidget::itemDoubleClicked, [this, kitchenData](QTreeWidgetItem *item, int column) { Kitchen_DoubleClicked(item, column, kitchenData); }
kitchenData is the Kitchen_Data instance you want to pass to your slot.
You really need to understand that QTreeVew does NOT have a signal like itemDoubleClicked(QTreeWidgetItem* item, int column ,Kitchen_Data &Kitchen) - it does not know anything about your own types. It only has itemDoubleClicked(QTreeWidgetItem* item, int column) signal...
-
@MostafaEzzat
One suggestion: you are using the old-styleSIGNAL
/SLOT()
macros. These often allow you to get away with incorrect things, which you do not discover till you get a runtime warning/error.Do yourself a favor and make yourself change over to New Signal Slot Syntax in any code you write. It will give you compile-time help on what is acceptable, and stop you doing things which won't work. You have to use this for lambdas anyway. The code @jsulm is showing you uses the new style.
-
@Christian-Ehrlicher @jsulm @JonB
Thanks for you guys it's working and the weird thing that it's working however the instance of the class passed by object not by reference because i insert in a function and delete in another function and the vector read both values well in both function
-
i'm sorry can you explain why we added the instance in [] and the other arguments in parameters
[this, kitchenData](QTreeWidgetItem *item, int column)
However the code here didn't mention that
connect( sender, &Sender::valueChanged, [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); } );
-
@MostafaEzzat
The code you show does have[=]
.[
...]
is the context for the lambda --- variables "captured"/available to be used inside its body.[=]
means all local variables (captured by value). Here the variablereceiver
is available in the lambda's body.The stuff inside the
(...)
are parameters. The slot signature needs to match the signal signature. The signal passes aconst QString &newValue
parameter to the slot.You should read up on C++ lambdas.
-
I'm sorry for opening this again but i need to pass the object of the class by reference i searched many times but i didn't find solution
connect(ui->treeWidget_Kitchen, &QTreeWidget::itemDoubleClicked, [this, kitchenData](QTreeWidgetItem *item, int column) { Kitchen_DoubleClicked(item, column, kitchenData); }
-
@MostafaEzzat
Which object of which class? Anyway, as you would expect an individual variable can be passed by reference via&
, e.g.[this, &kitchenData] () {}
if that's what you really want, or just as your original[=]
passes all local variables by value without having to enumerate them, so you can use[&]
to pass all local variables by reference instead. -
Sorry for bothering but i tried many ways .. Thanks in advance
Profile3::Profile3(QWidget *parent) : QWidget(parent), ui(new Ui::Profile3) { ui->setupUi(this); ui->tabWidget->tabBar()->setStyle(new CustomTabStyle ); ui->tabWidget_2->tabBar()->setStyle(new CustomTabStyle ); QTreeWidgetItem *item = new QTreeWidgetItem; int column; Kitchen_Data kitchenData; connect(ui->treeWidget_Kitchen, &QTreeWidget::itemDoubleClicked, [this, &kitchenData](QTreeWidgetItem *item, int column) { Kitchen_DoubleClicked(item, column, kitchenData); }); }
and this
Profile3::Profile3(QWidget *parent) : QWidget(parent), ui(new Ui::Profile3) { ui->setupUi(this); ui->tabWidget->tabBar()->setStyle(new CustomTabStyle ); ui->tabWidget_2->tabBar()->setStyle(new CustomTabStyle ); QTreeWidgetItem *item = new QTreeWidgetItem; int column; Kitchen_Data kitchenData; connect(ui->treeWidget_Kitchen, &QTreeWidget::itemDoubleClicked, [&](QTreeWidgetItem *item, int column,Kitchen_Data &kitchenData) { Kitchen_DoubleClicked(item, column, kitchenData); }); }
-
@MostafaEzzat Please read again the last post from @JonB - he told you what to do and why.
Before using a feature one should inform yourself about what it does instead simply copy'n'pasting everything. -
for sorry in this duration i can't go and learn a serie of 20-30 video of lambda c++ for just one line of code (for sure i'll have to in the future but i can't right now)
-
@MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:
20-30 video of lambda c++ for just one line of code
You could read documentation instead of watching "20-30" videos: https://en.cppreference.com/w/cpp/language/lambda
"for sure i'll have to in the future but i can't right now" - I don't understand. You seem to have time to ask this here and wait for an answer, but you don't have time to read about C++ lambdas? -
@MostafaEzzat
I'm not sure that I see what was wrong with the idea of yourconnect(ui->treeWidget_Kitchen, &QTreeWidget::itemDoubleClicked, [this, &kitchenData](QTreeWidgetItem *item, int column) { Kitchen_DoubleClicked(item, column, kitchenData); });
given that you have a
void Kitchen_DoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);
I would have thought that would work syntactically, it did not give a compile-time error did it? @Christian-Ehrlicher , @jsulm ?
However. Your
Kitchen_Data kitchenData;
is a local variable inProfile3::Profile3()
constructor. Once that exits it will go out of scope/be destroyed. While you have left it to be accessed in your slot on the double-click signal. That will lead to bad things when it gets called!Any such variables must persist for as long as the slot is connected. You probably intend
Kitchen_Data kitchenData
to be a member variable in classProfile3
?Additionally,
QTreeWidgetItem *item = new QTreeWidgetItem;
is just a single item created duringProfile3::Profile3()
. And not even shown added into theui->treeWidget_Kitchen
. It has no relation to theQTreeWidgetItem *item
in theQTreeWidget::itemDoubleClicked
signal. Similarly for theint column
local variable. So I don't know what they are doing. -
This post is deleted!