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. How to use QStackedwidget without using designer form ?Also should be capable of navigating between pages when button is clicked.
Forum Updated to NodeBB v4.3 + New Features

How to use QStackedwidget without using designer form ?Also should be capable of navigating between pages when button is clicked.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mahendra HD
    wrote on last edited by
    #1

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    // create main window
    QMainWindow *mainwindow = new QMainWindow() ;
    
    // create main widget
    QWidget *mainwidget = new QWidget() ;
    
    // create button
    QPushButton *button = new QPushButton("Button") ;
    button->setFixedWidth(200) ;
    
    // create stacked widget and add button
    QStackedWidget *stack = new QStackedWidget() ;
    stack->addWidget(button) ;
    stack->setCurrentIndex(0);
    
    // create layout
    QHBoxLayout *mainlayout = new QHBoxLayout() ;
    mainlayout->setMargin(5) ;
    mainlayout->addWidget(stack) ;
    mainwidget->setLayout(mainlayout) ;
    mainwindow->setCentralWidget(mainwidget) ;
    mainwindow->show();
    
    
    return a.exec();
    

    }

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums
      Your code is almost as expected, except you add the button as the actual Page.
      It's more likely you want a QWidget with a layout and then insert buttons to that layout.
      ELse BUtton is the page which is kinda odd :)
      So its much like you do with the mainwidget.

      Can I ask why you don't want to use UI(designer form class ) file?
      You can both use UI file and code and actually, UI file is just code that is run in the
      setupUI() function.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mahendra HD
        wrote on last edited by
        #3

        Reason i am not using Qt designer is, i am developing a application in which one page is having 400 radio button it is easy for me to develop a logic to handle 400 radio buttons when i do not use Qt designer.
        So please help me to navigate between pages using QStackedWidget when button is clicked.

        jsulmJ 1 Reply Last reply
        0
        • M Mahendra HD

          Reason i am not using Qt designer is, i am developing a application in which one page is having 400 radio button it is easy for me to develop a logic to handle 400 radio buttons when i do not use Qt designer.
          So please help me to navigate between pages using QStackedWidget when button is clicked.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mahendra-HD said in How to use QStackedwidget without using designer form ?Also should be capable of navigating between pages when button is clicked.:

          So please help me to navigate between pages using QStackedWidget when button is clicked

          See http://doc.qt.io/qt-5/qstackedwidget.html#currentIndex-prop

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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