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. StackedWidget will not accept a custom QWidget with addWidget
Forum Updated to NodeBB v4.3 + New Features

StackedWidget will not accept a custom QWidget with addWidget

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 148 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.
  • M Offline
    M Offline
    mortem
    wrote on 28 May 2024, 14:56 last edited by
    #1

    Hello,
    I might be missing something crucial here. I'm trying to create multiple "views" with StackedWidget by putting my custom QWidget class inside that StackedWidget. Now I created a new class called SearchView which is supposed to go in the StackedWidget as one of the "views". The problem is that StackedWidget->addWidget will not accept it even though this SearchView is inherited from QWidget (I created the SearchWidget with Qt designer form class from QTCreator). Am I not supposed to do this? If I have missed a relevant tutorial or page I am very sorry but I could not find an answer to this problem. Thank you for your time

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        Ui::SearchView *search_view = new Ui::SearchView();
        ui->stackedWidget->addWidget(search_view);
    
    

    searchview.h looks like this

    namespace Ui {
    class SearchView;
    }
    
    class SearchView : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit SearchView(QWidget *parent = nullptr);
        ~SearchView();
    
    private:
        Ui::SearchView *ui;
        void populateTable(QTableWidget *table);
    };
    
    

    The error in question is:
    mainwindow.cpp:12:34: Cannot initialize a parameter of type 'QWidget *' with an lvalue of type 'Ui::SearchView *'
    qstackedwidget.h:62:28: passing argument to parameter 'w' here

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 28 May 2024, 14:57 last edited by
      #2

      You want 'SearchView', not the uic generated class 'Ui::SearchView'

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • M mortem has marked this topic as solved on 28 May 2024, 15:04

      1/2

      28 May 2024, 14:56

      • Login

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