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. SetcentralWidget in a timer
Forum Update on Monday, May 27th 2025

SetcentralWidget in a timer

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.2k Views
  • 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.
  • S Offline
    S Offline
    sunil.nair
    wrote on 26 Dec 2014, 13:35 last edited by
    #1

    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();
    }
    

    }

    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Dec 2014, 22:41 last edited by
      #2

      Hi,

      Where's the code with your timer ?

      What does a run through the debugger tell you ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DBoosalis
        wrote on 27 Dec 2014, 09:17 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sunil.nair
          wrote on 29 Dec 2014, 04:37 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Dec 2014, 22:16 last edited by
            #5

            Suspends the program ? What do you mean ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sunil.nair
              wrote on 30 Dec 2014, 05:19 last edited by
              #6

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 30 Dec 2014, 23:49 last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on 31 Dec 2014, 03:21 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sunil.nair
                    wrote on 31 Dec 2014, 05:25 last edited by
                    #9

                    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

                    1 Reply Last reply
                    0

                    1/9

                    26 Dec 2014, 13:35

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved