Comparison of two distinct pointers



  • 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?


  • Moderators

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


  • Lifetime Qt Champion

    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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.