Creating an object of a class inside another class - possible?

This is a discussion on Creating an object of a class inside another class - possible? within the C++ Programming forums, part of the General Programming Boards category; Oops, sorry. :-P...

  1. #16
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Oops, sorry. :-P
    Just GET it OFF out my mind!!

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by audinue View Post
    Code:
    QObject::connect(&nextButton, SIGNAL(clicked()), &textToSpeechDialog1, SLOT(show()));
    ?

    Prefferably best to use Qt Designer, you can "Promote" whatever manual coded Qt widgets.

    About the factory that Elysia mentioned is boost library or std whose smart pointer to handle allocation and deallocation easily.

    Qt has one too called QPointer, however it's SLOWER than any other do.

    EDIT:
    It's QSharedPointer not QPointer :-P sorry.
    Yea, I wanted to use QT Designer, but was unable to find a place to download it from.
    Supposedly, its bundled with the free edition of QT (or so said someone on their forums), but I downloaded the latest QT SDK from their site, and it didn't include QT Designer or QT Assistant.

    I'm searching again right now, on Google...

    EDIT:

    And that's not the code I used. (I'll post the code in a few)
    Last edited by Programmer_P; 08-15-2009 at 09:21 AM.

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Yea, according to a post found at this link:

    where can i download free qt designer? - Qt Designer - QtForum.org

    its included with QT, but that is just not the case.
    I had hopes the link in the second post might be where I could download it from, but it says the page does not exist...

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Header:

    Code:
    #ifndef TTS_MOD_H
    #define TTS_MOD_H
    
    #include <QDialog>
    
    class QPushButton;
    
    class TextToSpeechDialog1 : public QDialog {
        Q_OBJECT
    
    public:
        TextToSpeechDialog1(QWidget* parent = 0);
    private slots:
        void nextClicked();
    private:
        QPushButton* nextButton;
    };
    
    #endif // TTS_MOD_H
    Implementation:

    Code:
    TextToSpeechDialog1::TextToSpeechDialog1(QWidget *parent)
    : QDialog(parent) {
    
    nextButton = new QPushButton(tr("&Next"));
        nextButton->setDefault(true);
    
    connect(nextButton, SIGNAL(clicked()),
    this, SLOT(nextClicked()));
    
    QHBoxLayout *bottomLayout = new QHBoxLayout;
        bottomLayout->addWidget(cancelButton);
    bottomLayout->addWidget(nextButton);
    
    QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addLayout(bottomLayout); 
    setLayout(mainLayout);
    
    setWindowTitle(tr("Text to Speech Wizard (Screen 1)"));
        setFixedHeight(sizeHint().height());
    }
    
    
    void nextDialog() {
       TextToSpeechDialog1* p = new TextToSpeechDialog1;
       p->hide(); //for some reason, though I wrote this line, the first dialog is not hidden
       TextToSpeechDialog2* dialog2 = new TextToSpeechDialog2;
       dialog2->show();
    
    }
    
    void TextToSpeechDialog1::nextClicked() {
    
        nextDialog(); //call the nextDialog()
    }

  5. #20
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Download from the leftmost link, the complete SDK, not the rightmost which is only the library.

    I love Qt because it is best and simple coding convention, just prefix anything with 'Q' or 'q' without any namespaces, although it isn't reusable and extensible enough to be used for big application.
    Just GET it OFF out my mind!!

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by audinue View Post
    Download from the leftmost link, the complete SDK, not the rightmost which is only the library.
    I did, but QT Designer was still not included in the SDK for some reason.
    There is a directory in the QT/version/bin folder called "designer", but it doesn't contain the .exe.
    EDIT:
    Oh, and I downloaded both the Windows and Linux versions, but neither included QT Designer.
    I love Qt because it is best and simple coding convention, just prefix anything with 'Q' or 'q' without any namespaces, although it isn't reusable and extensible enough to be used for big application.
    What do you mean by "big"? The application that I am writing in QT is going to be reasonably big (i'd say maybe 2,000 lines of code, not counting the included QT header files).
    Last edited by Programmer_P; 08-15-2009 at 10:17 AM. Reason: left something out

  7. #22
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    So the dialog1 show the new dialog2 and then the dialog1 is hidden?

    1. Make the dialog2 to be member of the 1 so it can be deleted.

    2. Just call hide() without creating new instance.
    Just GET it OFF out my mind!!

  8. #23
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    It should be in Qt/ver/qt/bin
    Just GET it OFF out my mind!!

  9. #24
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by audinue View Post
    So the dialog1 show the new dialog2 and then the dialog1 is hidden?

    1. Make the dialog2 to be member of the 1 so it can be deleted.

    2. Just call hide() without creating new instance.
    Not quite. The dialog 1 calls the global function (nextDialog()), which first hides (or is supposed to) the first dialog (through a new object of the class), and then a object of dialog 2 is created, and dialog2->show() is called, so it becomes visible. EDIT: Nevermind. Scratch that. I now see my mistake. I will need to call hide from the first instance in order to hide it, of course...it wont work from a new object.

    I have thought about making 2 a derived class of 1, but then I would have to go to extra work to parse the code of 1 that i don't need in 2, and there must be a way to get it to work without that. Maybe just jump back to int main(), and call the hide function from the first instance.
    Last edited by Programmer_P; 08-15-2009 at 10:45 AM.

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by audinue View Post
    It should be in Qt/ver/qt/bin
    Ah! So I do have it!
    Thank you very much for the info. It turns out QT Designer (along with Assistant) is right where you said it would be. Many thanks.
    Its weird that they didn't create shortcuts to it, though. /??
    Last edited by Programmer_P; 08-15-2009 at 10:43 AM.

  11. #26
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    The designer also "embedded" inside the creator, yet it can be launched inside the creator too.

    About the "delete this"

    Code:
    std::auto_ptr< YourClass<T> > thisIsUglierAndUnfrendlySyntaxYouEverUsed(void);
    Just GET it OFF out my mind!!

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    It's not unfriendly and it's not ugly. Compare it to something like this:

    void ThisIsADescriptiveFunctionName();
    void TIADFN();

    Which name is more descriptive here?
    If I see a function returning a shared pointer, I can rest assured that I won't have to do anything special with it or handle it with care.
    Also, delete this only works if it was allocated with new. And new is potentially 50x or more slower than creating the object on the stack. So why use it in the first place? Let the programmer be able to choose where to create the object.
    Also, auto_ptr is possibly bad in this case since it has exclusive ownership, not shared.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, my last assumption was almost correct. However, since the hide function would be called without hitting the next button (due to me putting it in int main, as is), I had to think of a different method.

    So what I did instead was I created a function (named sendHideReqest()) which I put the hide line in. I then simply passed "dialog" (the first dialog's class's object) into the function when I called it from int main. Unfortunately, the end result was the same.

    I tried using an if statement to control when the sendHideRequest() function is called, but for some reason, it seems to call it anyway (maybe due to the nature of if statements?), even before I push the next button of the first dialog.

    Here's the relevant code:
    Code:
    int nextDialog() {
       TextToSpeechDialog2* dialog2 = new TextToSpeechDialog2;
       dialog2->show();
       return 1;
    
    }
    void sendHideRequest(TextToSpeechDialog1* hide) {
        hide->hide(); //this line hides TextToSpeechDialog1
    }
    void TextToSpeechDialog1::nextClicked() {
        nextDialog(); //call the nextDialog()
    }
    
    int main(int argc, char* argv[]) {
        QApplication app(argc, argv);
        TextToSpeechDialog1* dialog = new TextToSpeechDialog1;
        dialog->show();
        if (nextDialog() == true) //the trouble line: it seems to by default call nextDialog()
          sendHideRequest(dialog); //pass the "dialog" object into the function
    
        return app.exec(); //hand control of the program over to QT
    
    }

  14. #29
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    So what happens is when the program opens, you only see the second dialog (the first dialog is already hidden). The code was supposed to wait until I press Next on the first dialog to call the second dialog (and subsequently hide the first dialog), but it seems to call nextDialog() as a result of the if line, and so the end result is only the second dialog is displayed.

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    But obviously, you just show the first window, then proceed to show the second window, then immediately return 1 (which really should be true and bool), and goes on to hiding the first dialog.
    You need to wait for some event that indicates that the "next" button has been pressed before proceeding. How that's done in Qt, I have no idea, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. array of class object
    By TomButcher in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:48 AM
  4. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 03:52 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21