Results 1 to 5 of 5

Thread: About mainwindows layout problem(segmentation fault)

  1. #1
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default About mainwindows layout problem(segmentation fault)

    i want to implement below function:

    mainwindow have two widget(A is stackwidget and B is QFrame).

    1. at first A is showed.
    2. when click QAction action_B, A is hided, B is showed.
    3. when click QAction action_A, A is showed, B is hided.

    when debugger.
    At first click action_A, can show A and hide B, but segmentation fault when i cllick action_B.

    i delete this->setCentralWidget(A) or this->setCentralWidget(B) , no fault was happened.

    if i want to implement function above what i said. how to program? please give me any advice. thank your for your help!


    codes see as below:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ...............
    8. this->setCentralWidget(A);
    9. }
    10.  
    11. void MainWindow::init();
    12. {
    13. ui->action_A->setEnabled(true);
    14. ui->action_B->setEnabled(false);
    15.  
    16. a->hide();
    17. ..........
    18. this->setCentralWidget(B);
    19. b->show();
    20. }
    21.  
    22. void MainWindow::on_action_A_triggered()
    23. {
    24. b->hide();
    25. a->show();
    26. ui->action_A->setEnabled(false);
    27. ui->action_B->setEnabled(true);
    28. ......
    29. }
    30.  
    31. void MainWindow::on_action_B_triggered()
    32. {
    33. init();
    34. }
    To copy to clipboard, switch view to plain text mode 


    TKS.

    ken
    Last edited by wysota; 8th December 2012 at 16:50. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: About mainwindows layout problem(segmentation fault)

    It looks like you want both of them to be in the central widget role of the main window.

    Why don't you just create a stack widget and put both A and B into it and then set the new stack widget as the central widget?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    kenchan (7th December 2012)

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

    Default Re: About mainwindows layout problem(segmentation fault)

    Why refuse to use code tags?

    Please respond to my latest post here http://www.qtcentre.org/threads/52310-Segment-fault

    mainwindow have two widget(A is stackwidget and B is QFrame).
    No it doesn't.


    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    ...............
    this->setCentralWidget(A);
    }
    Read my sig.
    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. The following user says thank you to amleto for this useful post:

    kenchan (7th December 2012)

  6. #4
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: About mainwindows layout problem(segmentation fault)

    Hi anda_skoa,

    create new stack widget can be achieved.

    but anther problem, several widgets into A. if i want the widget to be fullscreen that maybe have trouble.


    by the way, i have two method,but have some problem. please give me any advice.
    1. i use function as below, but info 'd_func() is private etc' when debugged.

    Qt Code:
    1. void QMainWindow::setCentralWidget(QWidget *widget)
    2. {
    3. if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
    4. d->layout->centralWidget()->hide();
    5. d->layout->centralWidget()->deleteLater();
    6. }
    7. d->layout->setCentralWidget(widget);
    8. }
    To copy to clipboard, switch view to plain text mode 

    2. use layout , and then use show and hide function to set the widget into central widget, but segmentation fault also.
    Qt Code:
    1. QHBoxLayout *layout = new QHBoxLayout();
    2. layout->addWidget(A);
    3. layout->addWidget(B);
    4. this->centralWidget()->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 


    Added after 46 minutes:


    Hi All,

    i have handled the problem.

    Thank your for your advice.

    Best Regard.

    ken
    Last edited by wysota; 8th December 2012 at 16:50. Reason: missing [code] tags

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

    Default Re: About mainwindows layout problem(segmentation fault)

    If there is an existing central widget, and you call setcentralwidget again, the previous one will be deleted! Your pointer will be invalid.

    Also,

    CODE TAGS
    Last edited by amleto; 7th December 2012 at 11:10.
    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.

  8. The following user says thank you to amleto for this useful post:

    kenchan (7th December 2012)

Similar Threads

  1. Replies: 2
    Last Post: 9th August 2011, 13:28
  2. debugging problem : segmentation fault
    By qureshi in forum Newbie
    Replies: 16
    Last Post: 8th June 2011, 11:21
  3. segmentation fault
    By navid in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2009, 11:40
  4. Segmentation Fault?!
    By r07f1 in forum Newbie
    Replies: 2
    Last Post: 11th April 2008, 15:10
  5. Replies: 2
    Last Post: 19th May 2007, 18:25

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.