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. Comparison of two distinct pointers

Comparison of two distinct pointers

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 2.0k Views 1 Watching
  • 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
    mallika.k.v
    wrote on last edited by
    #1

    I'm fresher .Started to learn QT programming.

    I'm trying to compare two pointers.

    Below is my progragram,

    @
    msgBox = new QMessageBox(this);

      QPushButton *connectButton = msgBox->addButton(tr("Connect"), QMessageBox::ActionRole);
    
      QPushButton *abortButton = msgBox->addButton(QMessageBox::Abort);
    
      msgBox->exec();
    

    if (msgBox->clickedButton() == connectButton) {
    msgBox->setText("exit"); // connect
    } else if (msgBox->clickedButton() == abortButton) {
    // abort
    qApp->quit();
    }
    @
    Error:
    comparison of two distinct pointers.im no getting what is the error is.

    can any one help me in this?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please use '@' tags when posting code on this forum. I've modified your post to add them.

      QMessageBox::clickedButton() returns a pointer to QAbstractButton, yet your connectButton is a QPushButton. I would not think that is a problem, but apparently it is. So, try this:
      @
      if (qobject_cast<QPushButton *>(msgBox->clickedButton()) == connectButton) {
      msgBox->setText("exit"); // connect
      } else if (qobject_cast<QPushButton *>(msgBox->clickedButton()) == abortButton) {
      // abort
      qApp->quit();
      }
      @

      (Z(:^

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

        Hi,

        Why note re-use the code from the documentation example ?
        @
        QMessageBox messageBox(this);
        QAbstractButton *disconnectButton =
        messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
        ...
        messageBox.exec();
        if (messageBox.clickedButton() == disconnectButton) {
        ...
        }@

        It uses QAbstractButton

        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
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on last edited by
          #4

          Why not just check the status returned by QMessageBox::exec()

          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