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. Sending INT from dialog back to main.cpp

Sending INT from dialog back to main.cpp

Scheduled Pinned Locked Moved General and Desktop
5 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.
  • C Offline
    C Offline
    Crossfire_Softwarez
    wrote on last edited by
    #1

    I have a main.cpp setup, and added a dialog, in my dialog I have

    [code]
    CardSelect QDialog;
    QDialog.setModal(true);
    if(QDialog.exec())
    {
    Card= //the return INT from Dialog//
    QMessageBox::information(0,"Success",QString::number(Card)); // ’this’ didn’t work so I used ‘0’//
    }
    [/code]
    And in my dialog named cardselect I have
    [code]
    void CardSelect::on_comboBox_highlighted(int index)
    {
    card = index;
    }

        void CardSelect::on_pushButton_clicked()
               {
                         // code to send card back to main//
               }[/code]    
    

    I know I must be missing something here, simple yet obvious to someone who knows c++ inside and out, but for me, I learned mostly on my own, and cannot figure this out, if you could help with this, I would be very grateful.

    Thank you

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BelenMuñoz
      wrote on last edited by
      #2

      I think you could use signals and slots.
      http://qt-project.org/doc/qt-4.8/signalsandslots.html

      Hope it helps you.

      Regards.

      Me casé con un enano pa jartarme de reí.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Adrien Leravat
        wrote on last edited by
        #3

        Hi Crossfire_softwarez,

        First there is a little misunterstanding with your CardSelect instance variable maybe. It probably inherits QDialog, but should not be named "QDialog" too. Prefer a name like "cardSelectDialog".

        Calling exec() will blocking until dialog is canceled/accepted/closed. To retrieve your value, the easiest way is to add an attribute to you CardSelect class, that you will set in you "on_pushButton_clicked()" method, and then call that attribute getter right after you called exec().

        Thus giving
        @
        // CardSelect declaraction (.h)
        class CardSelect : public QDialog
        {
        ...
        private:
        int m_cardValue;
        };
        @
        @
        // CardSelect implementation (.cpp)

        void CardSelect::on_pushButton_clicked()
        {
        m_cardValue = 1;
        // or m_cardValue = sender().property("cardValue"); to use Qt property system
        }

        int CardSelect::myCardValue() const
        {
        return m_cardValue;
        }
        @
        @
        // Main
        if(cardSelectDialog.exec())
        {
        int Card = cardSelectDialog.myCardValue();
        QMessageBox::information(0,"Success",QString::number(Card)); // ’this’ didn’t work so I used ‘0’//
        }
        @

        Note that exec() returns 1 only if user "accepts" dialog. Don't forget to init your private m_cardValue too in CardValue constructor.

        Adeneo Embedded - www.adeneo-embedded.com

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Adrien Leravat
          wrote on last edited by
          #4

          And this thread belongs to "General and Desktop":http://qt-project.org/forums/viewforum/10/ category :)

          Adeneo Embedded - www.adeneo-embedded.com

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            [quote author="Adrien Leravat" date="1358949397"]And this thread belongs to "General and Desktop":http://qt-project.org/forums/viewforum/10/ category :)[/quote]

            Moved.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            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