Important: Please read the Qt Code of Conduct -

Accessing attributes of Widgets

  • In the mainwindow.iu I have added, to the MainWindow, centralWidget a stackedWidget with multiple pages. There is also a pushbutton in the centralWidget. I have set the slot of the pushbutton which created an empty function in mainwindow.cpp. Now, I have to write the code to make each press of the button step forward through the pages of the stackedWidget. This requires that I get the current page or index of stackedWidget. Then I need to increment it and use the setCurrentIndex attribute.

    I cannot seem to do this! Can someone please give me the exact syntax for accessing these attributes?

    int currentPage = Ui_MainWindow::stackedWidget->currentIndex();

    seems to be the closest I can get.

  • @bart.hollis

    int currentPage = ui->stackedWidget->currentIndex();

    In your MainWindow.h, you have Ui::MainWindow *ui; as a private member variable. So you would use ui-> to access ui's members.

  • I get expected primary-expression before '->' token pointing at Ui->

  • Make sure it's lowercase

  • Well, DUH! You did, after all show it as lower case.
    Thanks a bunch!

  • Glad I could help :) For things like this, the documentation helps a ton as well as example projects. YouTube videos do pretty good as well.

  • It's good to know I won't make that mistake again!

Log in to reply