Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]qobject_cast<t> confusion
QtWS25 Last Chance

[SOLVED]qobject_cast<t> confusion

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manny
    wrote on last edited by manny
    #1

    hello,

    I was using an example out of a book callled "programming a gui with qt4 second edition" (running qt5). bassically in an example in which we were making a spreadsheet, the author used the following algorithm to open a file in the recent files section.

    void MainWindow::openRecentFiles()
    {
    if(okToContinue())
    {
    QAction *action = qobject_cast<QAction *>(sender());

           if(action)
           {
               //loadFile takes in the files name and loads the file
               loadFile(action->data().toString)
           }
        
        }
    

    }

    I know that sender() will get the value of the sender of the signal an return a pointer to the object that called it. qobject_cast will then use it's argument (sender()) to cast it to the specified type(QAction). however can't i just do the following:

        QAction *action = sender();
    

    any help is greatly appreciated

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      qobject_cast is more like a safe way of casting. When the conversion is not possible, it returns NULL. Doing like you said, an impossible conversion would raise a runtime error.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        No because sender() returns a QObject * and not the type of object that emitted the signal. You could also be calling that slot from different places like a QPushButton or even directly from the code thus adding more complexity on the handling inside your slot.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • M Offline
          M Offline
          manny
          wrote on last edited by
          #4

          Thanks for the replies, really helps :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved