Results 1 to 7 of 7

Thread: Adding Separate Widgets into Layout

  1. #1
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding Separate Widgets into Layout

    I've created 3 separate widgets(3different classes), I would like to add them into my QMainwindow in the 3 frames that I've created using the Qt designer. I cant seem to get it to work here is my main file hopefully someone could tell me whats wrong with it.

    the 3 different classes are secret, decode & pick.

    Thank you

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QMainWindow *MyAppWindow = new QMainWindow;
    5. Ui::MainWindow mainwindow;
    6. mainwindow.setupUi(MyAppWindow);
    7.  
    8. mainwindow.secretFrame->secret(QWidget *parent);
    9. mainwindow.boardFrame->decode(QWidget *parent);
    10. mainwindow.pickerFrame->pick(QWidget *parent);
    11.  
    12. MyAppWindow->show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding Separate Widgets into Layout

    well, fixing syntax error give this...

    mainwindow.secretFrame->secret(parent);
    mainwindow.boardFrame->decode(parent);
    mainwindow.pickerFrame->pick(parent);

    but since you dont have any 'parent' variable, or show what secret(), decode() and pick() do, it's impossible to tell you how to fix your code (which we cant see).


    if you want to add widget(s) to a frame, you could put them in a layout and add the layout to the frame
    Last edited by amleto; 27th May 2012 at 11:36.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Separate Widgets into Layout

    here is one of my widgets

    .h file
    Qt Code:
    1. class Decode : public QWidget
    2. {
    3. public:
    4. //constructor
    5. Decode(QWidget *parent = 0);
    6.  
    7. //declaration of private methods
    8. void decodepeg(QHBoxLayout *layout);
    9. string peg;
    10.  
    11. protected:
    12. void paintEvent(QPaintEvent *event); //PaintEvent
    13.  
    14. };
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. Decode::Decode(QWidget *parent):QWidget(parent)
    2. {
    3. QHBoxLayout *layout = new QHBoxLayout(parent);
    4. decodepeg(layout);
    5. decodepeg(layout);
    6. decodepeg(layout);
    7. decodepeg(layout);
    8. }
    9.  
    10. void Decode::decodepeg(QHBoxLayout *layout)
    11. {
    12. string pegs;
    13. Peg *peg = new Peg();
    14. cout<<"Choose a color for the SecretCode Pegs"<<endl;
    15. cin>>pegs;
    16. peg->colour(pegs);
    17. layout -> addWidget(peg);
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'm getting an error at this part
    Qt Code:
    1. mainwindow.secretFrame->secret(QWidget *parent);
    2. mainwindow.boardFrame->decode(QWidget *parent);
    3. mainwindow.pickerFrame->pick(QWidget *parent);
    To copy to clipboard, switch view to plain text mode 
    of the main.cpp file

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding Separate Widgets into Layout

    as I say - you get an error because that isnt valid c++.

    Are you trying to call the constuctor directy? Because you wont be allowed to do that either.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Separate Widgets into Layout

    is there a way to insert the entire class as a widget to the layout?

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding Separate Widgets into Layout

    yes.

    QLayout and its specialisations. read the docs.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding Separate Widgets into Layout

    Quote Originally Posted by ashboi View Post
    I've created 3 separate widgets(3different classes), I would like to add them into my QMainwindow in the 3 frames that I've created using the Qt designer.
    Then put them in the 3 frames you created in Designer using Designer. Drop a QWidget into each frame and promote it.

    You really need to earn some basic C++ because lines 8 through 10 are not even close to being useful. Here are some questions to ask yourself:
    • What type is Ui::MainWindow::secretFrame, boardFrame or pickerFrame?
    • What functions and members are available in that type of object?
    • Do those members include one called decode(), secret() or pick()?
    • Where do you think the methods decode(), secret() or pick() are supposed to come from?
    • What do they have to do with your class Decode? Does it have a method decode()?
    • How would you pass a widget object to a decode() function if you had one?


    As exercise you should read the code that uic generates from a simple Designer *.ui file.
    Also read the documentation on ways to use a Designer Ui in your application especially the last paragraph of the "Direct Approach" section on the limits this imposes.

Similar Threads

  1. Qt Designer Solved: Adding widgets to QTabWidget layout
    By koan in forum Qt Tools
    Replies: 1
    Last Post: 13th February 2011, 23:35
  2. Replies: 0
    Last Post: 12th December 2010, 05:09
  3. Replies: 6
    Last Post: 23rd August 2010, 17:56
  4. Replies: 5
    Last Post: 18th April 2010, 23:31
  5. Replies: 5
    Last Post: 18th March 2010, 09:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.