Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase



  • Hi ,

    I've TreeWidget and made a slot itemDoubleClicked from QtDesigner

    • void on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);

    and another slot widget

    • void Cancel_TreeWidget_Kitchen( Kitchen_Data &Kitchen );

    i just want to pass this object by reference and be able to use QVector because also when i declare the object inside the

    on_treeWidget_Kitchen_itemDoubleClicked and use it in Cancel_TreeWidget_Kitchen()
    

    it doesn't work properly so i think i need to pass it by reference when i declare it by reference i lose the slot in void

    on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);
    
    • Second Part

    i've a class for connecting to database should i declare ConnectDB() only once in every File I've in the project then use QSqlQuery inside every function normally ?
    and for example if there are two users looking for something in a Table and the query will take about 30 sec and second user his query will take about 2 sec so will the second will wait until the first one finished as long as it's in the same table or it will excutue both of them separately ?

    class MySqlDatabase : public QSqlDatabase
    {
    public:
        MySqlDatabase();
        QSqlDatabase MySqlDB;
    
        bool ConnectDB();
    
    private:
    
    };
    
     
    bool MySqlDatabase::ConnectDB(){
    MySqlDB  = QSqlDatabase::database();
        MySqlDB =   QSqlDatabase::addDatabase("QMYSQL", "FirstConnection");
        MySqlDB.setHostName("localhost") ;
        MySqlDB.setUserName("root");
        MySqlDB.setPassword("root");
        MySqlDB.setDatabaseName("clinic_database");
    
        if(MySqlDB.open())
       {
    
               qDebug() << "connected sqldatabase";
           return true ;
    
       }
       else
        {
           qDebug() << "Error in opneing " << MySqlDB.lastError().text();
           return false;
        }
    
    
    }
    

  • Lifetime Qt Champion

    Do not derive from QSqlDatabase. Please follow the documentation



  • @Christian-Ehrlicher

    Thanks for Answering

    i mean should i use connecting only one time in the whole Header File or cpp file or use in every function i want to make query in ?

    if yes what's is the best way to make a union connection to database

    but actually i concern the first part of treeWidget coz it's important for me right now ^^

    Thanks in advance


  • Lifetime Qt Champion

    @MostafaEzzat said in passing argument in void on_treeWidget_Kitchen_itemDoubleClicked() not working and make union QSqlDatabase:

    i mean should i use connecting only one time in the whole Header File or cpp file or use in every function i want to make query in ?

    Why do you want to connect more than once?



  • @Christian-Ehrlicher

    For Example if i've 2 users on the same network quering database and want to make a connection for each one of them , and if they quering same table and the first user query will take about 1 minute (just a while) and second user his query takes 10 sec will the second user have to wait the first user until his query finished ?
    or they both will do the query whatever the time of the query of each one of them


  • Lifetime Qt Champion

    Don't see what this question will help when you open more than one database connection in your app.

    The new question is more a question for the database, not for Qt. But can you imagine how bad a database would scale if it could only handle one connection / query at a time?



  • @Christian-Ehrlicher

    Yeah i think you right it's relating to database

    so i just need to pass a object of class by reference in Slot of Double Clicked TreeWidget to access the data of the QVector that i entered

     void on_treeWidget_Kitchen_itemDoubleClicked(QTreeWidgetItem *item, int column,Kitchen_Data &Kitchen);
    

    i Aleardy have the slot but when i add the argument to the function parameter i lose the slot i ain't know why this happenes


  • Lifetime Qt Champion

    I don't know what your problem with the signals and slots is since I don't understand what you're writing. Please post some code and read the documentation about signals and slots.



  • @Christian-Ehrlicher

    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)));
    }
    

  • Lifetime Qt Champion

    You have to do the connect by yourself. Where is your corresponding signal? Where do you emit it?



  • @Christian-Ehrlicher

    i didn't emit i selected Double Clicked singlan from here then i added the argument for the functions manually

    Capture.PNG


  • Lifetime Qt Champion

    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.



  • @Christian-Ehrlicher

    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


  • Lifetime Qt Champion

    @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.



  • @Christian-Ehrlicher

    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


  • Lifetime Qt Champion

    @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.


  • Lifetime Qt Champion

    @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).



  • @jsulm @Christian-Ehrlicher

    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


  • Lifetime Qt Champion

    @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-style SIGNAL/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



  • @jsulm

    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 variable receiver 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 a const QString &newValue parameter to the slot.

    You should read up on C++ lambdas.


Log in to reply