Results 1 to 17 of 17

Thread: Wrong C++ syntax

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Wrong C++ syntax

    This is very basic C++ stupid question.
    Can somebody explain to me what I am doing wrong and / or correct my syntax so it will compile?



    Qt Code:
    1. QObject::connect(ui->list, //object
    2. &QListWidget::itemClicked(&item), //signal
    3. ui->list_2, ///object
    4. &QListWidget::addItem(&item)); //slot
    5.  
    6.  
    7. // Edit
    8. // After looking at some examples I have changed my code
    9.  
    10. // <pre lang="c++">
    11. QObject ::connect(ui->list,
    12. QListWidget.itemClicked(item),
    13. ui->list_2,
    14. QListWidget.addItem(item)
    15. );
    To copy to clipboard, switch view to plain text mode 


    Fist "style " gives this error

    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:141: error: cannot call member function 'void QListWidget::itemClicked(QListWidgetItem*)' without object
    2. &QListWidget::itemClicked(&item), //signal
    3. ^
    To copy to clipboard, switch view to plain text mode 

    Then I get this one

    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:153: error: expected primary-expression before '.' token
    2. QListWidget.itemClicked(item),
    3. ^
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Wrong C++ syntax

    You are mixing up the two different styles of the connect() statement, the "old style", which used signal and slot names with arguments, and the "new style" which uses addresses of member functions. And in the old style, you do not use the names of variables as arguments, you use the type of the variable. So you can write either this:

    Qt Code:
    1. // Old style
    2.  
    3. QObject::connect(ui->list, //object
    4. SIGNAL( "itemClicked( QListWidgetItem * )" ), //signal
    5. ui->list_2, ///object
    6. SLOT( "addItem( QListWidgetItem * )" ) ); //slot
    To copy to clipboard, switch view to plain text mode 

    or this:

    Qt Code:
    1. // New style
    2.  
    3. QObject::connect(ui->list, //object
    4. &QListWidget::itemClicked, //signal
    5. ui->list_2, //object
    6. &QListWidget::addItem ); //slot
    To copy to clipboard, switch view to plain text mode 

    EXCEPT in this particular case, the "old style" will fail at run time because QListWidget::addItem() is not a slot so Qt's meta-object system will not be able to match up the "addItem()" method with anything declared as a slot in the QListWidget class.

    The "new style" will work, because the matching of signal and slot is done at compile time, and a connect() statement in the new style can be between any two compatible methods, even an ordinary non-class method or a lambda.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Wrong C++ syntax

    This is what I have been getting while using the "new style "

    /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:140: error: no matching function for call to 'DeviceDiscoveryDialog::connect(QListWidget*&, void (QListWidget::*)(QListWidgetItem*), QListWidget*&, <unresolved overloaded function type>)'
    &QListWidget::addItem ); //slot
    ^

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Wrong C++ syntax

    This is because there are two QListWidget::addItem() methods, and the compiler doesn't know which one you want to use. So you have to be explicit in these cases, and there are two versions, one which works in C++11 or newer, then other requires C++14 or newer:

    Qt Code:
    1. // New style, C++11 and up version
    2.  
    3. QObject::connect(ui->list, //object
    4. &QListWidget::itemClicked, //signal
    5. ui->list_2, //object
    6. QOverload< QListWidgetItem * >::of( &QListWidget::addItem ) ); //slot
    7.  
    8.  
    9. // New style, C++14 and up version
    10.  
    11. QObject::connect(ui->list, //object
    12. &QListWidget::itemClicked, //signal
    13. ui->list_2, //object
    14. qOverload< QListWidgetItem * >( &QListWidget::addItem ) ); //slot
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Wrong C++ syntax

    I am sorry, but I have posted my question on so many forums I lost track where I stand.So if this is a dupe, please forgive me.

    I have C++11 (checked) and the compiler complains about this code :

    Qt Code:
    1. // New style, C++11 and up version
    2.  
    3. QObject::connect(ui->list, //object
    4. &QListWidget::itemClicked, //signal
    5. ui->list_2, //object
    6. QOverload< QListWidgetItem * >::of( &QListWidget::addItem ) ); //slot
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:161: error: 'QOverload' was not declared in this scope
    2. QOverload< QListWidgetItem * >::of( &QListWidget::addItem ) ); //slot
    3. ^
    To copy to clipboard, switch view to plain text mode 

    Adding QtGlobal did not resolve the issue.

    After some research I found that "QOverload should be used directly in C++11 ".
    Obviously I am having hard time undertaking "normal " "new syntax" of "connect" and have no clue how to apply QOverload directly.

    More help would be greatly appreciated.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Wrong C++ syntax

    From the Qt docs:

    This function was introduced in Qt 5.7.
    Are you using Qt 5.7 or later? If not, then the only workarounds you have are 1) declare a slot in your class and connect that to itemClicked() and in that slot, call ui->list_2->addItem(). Or 2) use a lambda expression instead of a slot. Or, of course, 3) upgrade to Qt 5.7 or later.

    Qt Code:
    1. // Case 1
    2.  
    3. // New style, C++11 and up version
    4.  
    5. QObject::connect(ui->list, //object
    6. &QListWidget::itemClicked, //signal
    7. this, //object
    8. &MainWindow::doAddItem ) ); //slot
    9.  
    10. //...
    11.  
    12. void MainWindow::doAddItem( QListWidgetItem * pItem )
    13. {
    14. ui->list_2->addItem* pItem );
    15. }
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. // Case 2
    2.  
    3. // New style, C++11 and up version
    4.  
    5. QObject::connect(ui->list, //object
    6. &QListWidget::itemClicked, //signal
    7. this, //object
    8. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ) } ); // lambda slot
    To copy to clipboard, switch view to plain text mode 

    However, you do realize what any of the methods you are trying will do, right? A QListWidgetItem can only belong to one QListWidget, so by calling addItem() on the second widget you are moving the QListWidgetItem from ui->list to ui->list_2. At the end of the slot, both list widgets will be updated and the item will disappear from ul->list and appear in ui->list_2.
    Last edited by d_stranz; 8th December 2020 at 22:09.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Wrong C++ syntax

    SOrry , cut and paste this:

    Qt Code:
    1. QObject::connect(ui->list, //object
    2. &QListWidget::itemClicked, //signal
    3. this, //object
    4. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ) } );
    To copy to clipboard, switch view to plain text mode 

    trows this error


    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:180: error: expected ';' before '}' token
    2. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ) } );
    3. ^
    To copy to clipboard, switch view to plain text mode 

    I have this :

    Qt Code:
    1. Qt Creator 3.5.1
    2. Based on Qt 5.5.1 (GCC 5.2.1 20151129, 64 bit)
    To copy to clipboard, switch view to plain text mode 

    Could that be part of my problem ??

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Wrong C++ syntax

    No the problem is that you aren't thinking about the errors you see before you fire off another post. The compiler is telling you that I forgot to put a semicolon at the end of the addItem( pItem ) call. It's even pointing at the place where the error is. Put a semicolon between the ) and the } and it will compile. I do the best I can to post correct code, but sometimes typos creep in.

    Qt Creator 3.5.1
    Based on Qt 5.5.1 (GCC 5.2.1 20151129, 64 bit)
    This is not telling you which version of Qt you have configured in your kit, it is telling you which version of Qt was used to build the Qt Creator software. The Qt versions used by the kit and by Qt Creator could be completely different. Look at the kit settings and that will tell you which Qt version you are compiling and linking against.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Wrong C++ syntax

    I have briefly scanned this
    Qt Code:
    1. https://en.cppreference.com/w/cpp/language/lambda
    To copy to clipboard, switch view to plain text mode 
    link
    and managed to find the missing ";" .

    Now I am getting this error

    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:181: error: no matching function for call to 'DeviceDiscoveryDialog::connect(QListWidget*&, void (QListWidget::*)(QListWidgetItem*), DeviceDiscoveryDialog*, DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget*)::<lambda(QListWidgetItem*)>)'
    2. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ); }) ;
    3. ^
    To copy to clipboard, switch view to plain text mode 

    I think I can fix that myself, (perhaps intelisense will help ) but I need to quit for today.
    Thanks for your help.


    Qt Code:
    1. QObject::connect(ui->list, //object
    2. &QListWidget::itemClicked, //signal
    3. this, //object
    4. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ); }) ;
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Wrong C++ syntax

    Sorry, I am typing faster than I am thinking. Try this:

    Qt Code:
    1. QObject::connect(ui->list, //object
    2. &QListWidget::itemClicked, //signal
    3. [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ); }) ; // lambda slot
    To copy to clipboard, switch view to plain text mode 

    There is no "receiver" pointer (the normal third argument to connect()) when using a connect() with the lambda syntax.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. #11
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Wrong C++ syntax

    Sorry, the original working XML code I am trying to test duplicate works between list and plain text .
    However, even after modifying your lambda to plain text I get a same error - compiler does not like the "copy to" function.

    Qt Code:
    1. QObject::connect(ui->list, //object
    2. &QListWidget::itemClicked, //signal
    3. [this]( QPlainTextEdit * pItem ) { this->ui->plainTextEdit->appendPlainText( pItem ); }) ; // lambda slot
    4. // [this]( QListWidgetItem * pItem ) { this->ui->list_2->addItem( pItem ); }) ; // lambda slot
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. /media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:154: error: no matching function for call to 'QPlainTextEdit::appendPlainText(QPlainTextEdit*&)'
    2. [this]( QPlainTextEdit * pItem ) { this->ui->plainTextEdit->appendPlainText( pItem ); }) ; // lambda slot
    3. ^
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Using the Qt5 connecy syntax
    By bnosam in forum Newbie
    Replies: 3
    Last Post: 9th February 2015, 02:57
  2. Regarding syntax
    By Yayati.Ekbote in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2010, 14:15
  3. Odd Syntax
    By acxdotfm in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 20:23
  4. wrong syntax or QTcpSocket problem?
    By vito49 in forum Newbie
    Replies: 3
    Last Post: 8th October 2008, 08:12
  5. Syntax Highlighter sample is wrong
    By kib2 in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2007, 21:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.