SetcentralWidget in a timer



  • Hello, I am setting the the setCentralWidget as Mdi Area mainDisplay. I create a button 'go back'. Which is connected to the timer. The timer calls mainMenu whenever goback is clicked. inside, mainMenu public slot: I setCentralWidget as mainDisplay but it says

    "program has stopped working"....setCentralWidget works fine when written in another public slot on PushButoon1 clicked. What should I do?

    @

    const QSize buttonsize(50,50);
    const QSize scrollareasize(400,400); //minimum size of the catalogue subwindow

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

    mainlayout= new QVBoxLayout;
    displaywidget= new QHBoxLayout;
    feeltexture = new QVBoxLayout;
    goback= new QPushButton("Go BAck");
    QObject::connect(goback, SIGNAL(clicked()), this,  SLOT(mainMenu()));
    
    
    std::cout<<"reached mainwindow constructor"<<std::endl;
    
    
    
    QLabel *label = new QLabel("<h1><font color = RED> Tangible textures </font> </h1>");// create the application title
    
    //create the icons for all the UI push button and resize the buttons
    ui->pushButton->setIcon(QIcon(":/pattern4.jpg"));
    ui->pushButton->resize(buttonsize);
    
    //creating catalogue and adding the buttons in vertical box layout
    catalogue = new QWidget;
    QHBoxLayout *row0= new QHBoxLayout;
    row0->addWidget(label);
    QHBoxLayout *row1= new QHBoxLayout;
    row1->addWidget(ui->pushButton);
    
    
    //add all widgets to the main layout
    mainlayout->addLayout(row0);
    
    //add patterns to the catalogue widget. Set the catalogue widget in a scroll area
    subwindow= new QVBoxLayout;
    scrollregion= new QScrollArea;
    subwindow->addLayout(row1);
    
    catalogue->setLayout(subwindow);//add pattern icons to catalogue
    scrollregion->setWidget(catalogue);
    scrollregion->setMinimumSize(scrollareasize);
    scrollregion->setWidgetResizable(true);//set the widget as resizable
    scrollregion->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//always show horizontal scrollbar
    scrollregion->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//always show vertical scrollbar
    scrollregion->viewport()->setBackgroundRole(QPalette::Dark);
    scrollregion->viewport()->setAutoFillBackground(true);
    
    //set the the central widget of UI as the mainlayout
    mainlayout->addWidget(scrollregion);
    
    mainDisplay= new QMdiArea;//mainDisplay is the mdi Area
    textureDisplay= new QMdiArea;
    subwindow1= new QMdiSubWindow;
    subwindow1->setWidget(scrollregion);
    subwindow1->setMinimumHeight(580);
    mainDisplay->addSubWindow(subwindow1);
    setCentralWidget(mainDisplay);
    

    }
    void MainWindow::mainMenu()
    {
    setCentralWidget(mainDisplay);
    }
    void MainWindow::on_pushButton_clicked()
    {
    //if this is the first button to be clicked on the UI
    if(view==NULL)
    {
    view=new CubeView();
    //view->begin(4);
    view->resize(800, 600);

        feeltexture->addWidget(goback);
        textureDisplay->setLayout(feeltexture);
        setCentralWidget(textureDisplay);
        //view->show();
    }
    

    }

    @


  • Lifetime Qt Champion

    Hi,

    Where's the code with your timer ?

    What does a run through the debugger tell you ?



  • Rather then switching the central widget of the mainwindow, I would suggest you use a QStackedWidget as the central widget then add your widgets to the QStackedWidget. And intead of manageing another object (QTimer). Use the QObject::timerEvent() in your mainwindow



  • Yeah, I tried that only DBoosalis. Setting the centralwidget uspends th program. ratgher, I now initialize all the subwindows inthe constructor and the hide/show thrm as and when required. Thank you both.


  • Lifetime Qt Champion

    Suspends the program ? What do you mean ?



  • The program hangs and shows the the program is not working. In the debugger it shows, Invalid parameter passed to C runtime function.


  • Lifetime Qt Champion

    Pretty strange, can you show the code you where using for that ?



  • When you call setCentralWidget() any existing central widget is deleted "at the approriate time" (possibly immediately) according to the docs. Your approach will rapidly generate dangling pointers and crash when you next use one of them.

    You should be using QStackedWidget.



  • Oh. Okay. I will check and post the same.

    @SGaist: The code is the same that I used above. setCentralWidget in the constructor as well as in the Main menu slot


Log in to reply
 

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