Results 1 to 17 of 17

Thread: Wrong C++ syntax

  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
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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 

  12. #12
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    464
    Thanks
    10
    Thanked 61 Times in 59 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Wrong C++ syntax

    Hi, your connect doesn't work because you use the itemClicked signal, which emits a pointer to a QListWidgetItem, as in d_stranz' code. But you try to connect it to a slot that takes a QPlaintextEdit*. The argument for your lambda slot needs to be the same type as the type that the signal emits.

    Ginsengelf

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Wrong C++ syntax

    However, even after modifying your lambda to plain text I get a same error - compiler does not like the "copy to" function.
    Come on, quit screwing around here. Your original post asked how to connect a signal and slot between two QListWidget instances. The final code I posted would do that, using a lambda function instead of an actual slot. Now you say, I didn't want to connect to another list widget after all, I wanted to connect the list widget to a QPlainTextEdit.

    You need to remember some basic C++. Signals and slots are no different from ordinary methods of C++ classes. They are labeled with "signals:" and "slots:" keywords in Qt so that the MOC compiler can recognize them and create the code in the moc_*.cpp file that implements them and makes the first style of connect() work at runtime. A lambda function is just a fancy way to write a nameless C++ function that gets compiled in place.

    All the rules of C++ apply to signals, slots, and lambdas: If you want to call one of these functions, you have to supply the correct number and types of arguments to it. You can't call a function that expects one type of pointer using an argument that is a different type. It's basic C++, pure and simple.
    <=== 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.

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

    Default Re: Wrong C++ syntax

    Quote Originally Posted by d_stranz View Post
    Come on, quit screwing around here. Your original post asked how to connect a signal and slot between two QListWidget instances. The final code I posted would do that, using a lambda function instead of an actual slot. Now you say, I didn't want to connect to another list widget after all, I wanted to connect the list widget to a QPlainTextEdit.

    You need to remember some basic C++. Signals and slots are no different from ordinary methods of C++ classes. They are labeled with "signals:" and "slots:" keywords in Qt so that the MOC compiler can recognize them and create the code in the moc_*.cpp file that implements them and makes the first style of connect() work at runtime. A lambda function is just a fancy way to write a nameless C++ function that gets compiled in place.

    All the rules of C++ apply to signals, slots, and lambdas: If you want to call one of these functions, you have to supply the correct number and types of arguments to it. You can't call a function that expects one type of pointer using an argument that is a different type. It's basic C++, pure and simple.
    Sorry, but my objective, in case I did not say it - is to replace QtDesigner signal / slot in XML with C++ code.
    The XML works by coping a string from list and appending it to plain text.

    I initially started with list to list and have been getting "invalid function " when using "addItem", even when intelisense lets me use "addItem".
    So I assumed I am using wrong syntax and still like to verify my mistake, irregardless if passing between item or plain text.
    At this point I am not interested in bypassing my error using lambda - if XML works so should new or old style C++ "connect" work.

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Wrong C++ syntax

    I am not interested in bypassing my error using lambda
    Using a lambda expression does not bypass anything. It is simply an alternative to defining an actual slot or other method with the correct function signature. In your original post with two list widgets, you couldn't use the new style version of connect() that uses the QOverload<> syntax because your version of Qt is probably too old, and you couldn't use the old style version because it requires addItem() to be a slot and it isn't.

    Now in your new code you are trying to connect a signal that sends an apple to a slot that wants an orange. Your error seems to be in thinking that somehow you can connect something to anything else and arguments will magically get converted. As I said already, signals and slots are just C++ functions, and you can't call a C++ function with the wrong number or types of arguments.

    The XML works by coping a string from list and appending it to plain text.
    How is that possible? There is no itemClicked() signal from QListWidget that sends a text string, so it is not possible in QtDesigner or anywhere else to connect a QListWidget itemClicked() signal to -any- slot that expects a text string as an argument. The ui XML code will compile, because the UIC and MOC compilers will create the old style connect() statement in its code, but it will not work at run time because the function signatures do not match.

    The only QListWidget signal that returns a string is currentTextChanged(). So the only possible working signal / slot connection you could have made in QtDesigner would be to connect QListWidget::currentTextChanged() to QPlainTextEdit::appendPlainText(), and that will result in whatever text was clicked on in the list widget getting copied and appended to the text edit. Neither the signal nor the slot are overloaded methods, so you could use either the old or new style of connect() to make the connection.

    Qt Code:
    1. connect( ui->list, SIGNAL( currentTextChanged( const QString & ) ), ui->plainTextEdit, SLOT( appendPlainText( const QString & ) ) );
    2.  
    3. // or
    4.  
    5. connect( ui->list, &QListWidget::currentTextChanged, ui->plainTextEdit, &QPlainTextEdit::appendPlainText );
    To copy to clipboard, switch view to plain text mode 

    In any case, there is no execution of XML code. The XML file edited by QtDesigner is simply instructions to the UIC and MOC compilers that tell them how to generate the C++ code that defines the user interfaces and instantiate it at run time, including making any signal and slot connections that have been defined in the ui file. That C++ code will always compile because connect() statements that get generated are the old style, which use method names as strings and not as addresses of member functions (as the new style does). New style connect() statements will fail to compile if the functions do not match. Old style connect() will compile, but at run time will fail because Qt's meta-object system will not be able to match up the function at that time.
    <=== 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.

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

    Default Re: Wrong C++ syntax

    Partial success.
    Using both old and new "connect" style throws "segmentation fault" at either one.

    It is my understanding that "connect" code placement is not critical - however my crash occurs exactly on "connect" which I have placed in a secondary dialog constructor. My main window dialog is primary.

    RTFM implies that passing null pointer may be the problem...

  17. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Wrong C++ syntax

    It is my understanding that "connect" code placement is not critical
    No, not true. You must connect the signal and slot before any of the signals you want to process can be issued. The customary place to make the connection is in the constructor of the parent of the two widgets you want to connect. This ensures that the connection is made only once and before either of the connected widgets can do anything. There are other places, but this is the most common.

    RTFM implies that passing null pointer may be the problem...
    Almost always either a null pointer or a pointer that has not been initialized. If you are passing a pointer in to your secondary dialog via a function call, then calling connect() using the variable that holds that pointer before you have made that call (i.e. in the dialog constructor) means you are using an uninitialized pointer.
    <=== 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.

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.