Unsolved passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase
-
I made the slot from QT Designer and This the declaration and the function itself the problem when i want to pass Kitchen_Data &Kitchen by reference the qDebug() prints this
QMetaObject::connectSlotsByName: No matching signal for on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem*,int,Kitchen_Data&)
- below Declaratinon and the function which is inside the function not important but i just want to know why i lost the slot Thanks in advnace
void on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);
void Profile3::on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen) { itemList_Analysis = ui->treeWidget_Kitchen->selectedItems(); QString MealCode ; QString MealName ; QString MealPrice ; QString MealType; foreach( item, itemList_Analysis){ MealCode = item->text(0); MealName = item->text(1); MealPrice = item->text(2); MealType = item->text(3); KitechenItemName.push_back(MealName) ; Kitchen.KitechenItemPrice.push_back(MealPrice); qDebug() << Kitchen.KitechenItemPrice.at(0) << " PRicce" ; TotalKitchenPrice_Int += MealPrice.toInt(); QString TotalPriceAnalysisItems = QString::number(TotalKitchenPrice_Int); } qDebug() << KitechenItemName.size() << "inserting"; qDebug() << Kitchen.KitechenItemPrice.size() << "inserting"; // QVector <QString> KitechenItemPrice; // QVector <QString> KitechenItemName; // QVector <QString> All_Added_ItemOfKitechen; if(! KitechenItemName.empty()) { for(int i=0;i< KitechenItemName.size();i++){ qDebug()<< KitechenItemName[i]; qDebug()<< Kitchen.KitechenItemPrice[i]; } } qDebug() << "Done " ; QTreeWidget * tree = ui->treeWidget_ShowKitchen; Kitchen.itemm_CancelButton_From_Kitchen = new QTreeWidgetItem(); QPushButton * CancelButton_TreeWidget_2 = new QPushButton("Cancel"); Kitchen.itemm_CancelButton_From_Kitchen->setText(0,MealCode); Kitchen.itemm_CancelButton_From_Kitchen->setText(1,MealName); Kitchen.itemm_CancelButton_From_Kitchen->setText(2,MealPrice); Kitchen.itemm_CancelButton_From_Kitchen->setText(3,MealType); tree->addTopLevelItem(Kitchen.itemm_CancelButton_From_Kitchen); tree->setItemWidget(Kitchen.itemm_CancelButton_From_Kitchen, 4, CancelButton_TreeWidget_2); connect(CancelButton_TreeWidget_2, SIGNAL(clicked(bool)), this, SLOT( Cancel_TreeWidget_Kitchen(Kitchen_Data &Kitchen))); }
-
You have to do the connect by yourself. Where is your corresponding signal? Where do you emit it?
-
i didn't emit i selected Double Clicked singlan from here then i added the argument for the functions manually
-
But QTreeWidget has no signal which emits a Kitchen_Data &Kitchen as third argument - so how should this work. You have to define a signal by your own and connect it. Please read the documentation.
-
why did it even emit when i add the argument to the built-infunction definition of QT
but i guess i'll just create new one manually from QT Desginer
-
@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)