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. QWidget subclass does not appear on main window.

QWidget subclass does not appear on main window.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 687 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.
  • clostridium_difficileC Offline
    clostridium_difficileC Offline
    clostridium_difficile
    wrote on last edited by
    #1

    Hello, I want to create my own, custom ToolBar. For now, ToolBar is just a simple rectangle area with background. My problem concerns that, when I add ToolBar to main window class, it does not appear on screen. How to make it work?
    ToolBar declaration:

    class ToolBar : public QWidget
    {
    	Q_OBJECT
    
    public:
    	explicit ToolBar(QWidget * parent = nullptr);
    
    	[[nodiscard]] QSize sizeHint() const override;
    
    private slots:
    
    	void ButtonPressed();
    
    private:
    	std::vector<QPushButton*> buttons;
    
    	void AddButton();
    
    };
    
    ToolBar::ToolBar(QWidget * parent) : QWidget(parent), buttons(0)
    {
    	move(50, 100);
    	setFixedSize(sizeHint());
    	setStyleSheet("background-color: #123456;");
    
    	qDebug() << "tworze toolbar";
    }
    
    QSize ToolBar::sizeHint() const
    {
    	return QSize(400, 400);
    }
    

    MainWindow definition:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        
        ~MainWindow();
    
        void Show();
    
    private slots:
    	void CreateNewProject();
    
    	void NewCanvas(const QString & string);
    
    	void FirstButtonClicked();
    
    private:
        Ui::MainWindow *ui;
    
    	ToolBar * toolBar;
    
    	QPushButton * button;
    
    	QWidget * centralWidget;
    
        void closeEvent(QCloseEvent * closeEvent) override;
    
        void resizeEvent(QResizeEvent * resizeEvent) override;
    
        void moveEvent(QMoveEvent * moveEvent) override;
    
    	void mouseReleaseEvent(QMouseEvent * event) override;
    };
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        centralWidget(new QWidget(this))
    {
    	QSettings settings;
    	int width, height;
    	int xPos, yPos;
    
    	ui->setupUi(this);
    
    	toolBar = new ToolBar(this);
    	toolBar->show();
    
        button = new QPushButton(this);
        button->setFixedSize(50, 50);
        button->move(400, 400);
        button->setText("MW");
    
        width = qApp->screens()[0]->size().width() / 2;
        height = qApp->screens()[0]->size().height() / 2;
        width = settings.value("window/width", width).toInt();
        height = settings.value("window/height", height).toInt();
        xPos = settings.value("window/xpos", 0).toInt();
        yPos = settings.value("window/ypos", 0).toInt();
    
        setGeometry(xPos, yPos, width, height);
    }
    
    void MainWindow::closeEvent(QCloseEvent *closeEvent)
    {
    	QSettings settings;
    	settings.setValue("window/width", width());
    	settings.setValue("window/height", height());
    	settings.setValue("window/xpos", pos().x());
    	settings.setValue("window/ypos", pos().y());
    	settings.sync();
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You have to add it to your layout. Best here is, since you're already using a ui file, to promote your widget in designer.

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

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

        Hi
        Its just invisible as the stylesheet is not applied.

        try adding

        protected:
            virtual void paintEvent(QPaintEvent *event) override
            {
                QPainter p(this);
                p.drawRect(0, 0, width() - 1, height() - 1);
            }
        

        and you see its there.

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          You have to add it to your layout. Best here is, since you're already using a ui file, to promote your widget in designer.

          clostridium_difficileC Offline
          clostridium_difficileC Offline
          clostridium_difficile
          wrote on last edited by
          #4

          @Christian-Ehrlicher I added widget to mainwindow and promoted it to ToolBar, but still it isn't visible

          Christian EhrlicherC 1 Reply Last reply
          0
          • clostridium_difficileC clostridium_difficile

            @Christian-Ehrlicher I added widget to mainwindow and promoted it to ToolBar, but still it isn't visible

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @clostridium_difficile The move(50, 100) is useless / counter-productive. Please show us your current code.

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

            clostridium_difficileC 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @clostridium_difficile The move(50, 100) is useless / counter-productive. Please show us your current code.

              clostridium_difficileC Offline
              clostridium_difficileC Offline
              clostridium_difficile
              wrote on last edited by clostridium_difficile
              #6

              @Christian-Ehrlicher current code? I showed you my current code in my first post in this topic and nothing changed. Widget has been promoted in Qt Designer. I moved toolbar to (50, 100), since I wanted to see what happens.
              @mrjj i applied style sheet in line

              setStyleSheet("background-color: #123456;");
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @clostridium_difficile said in QWidget subclass does not appear on main window.:

                I showed you my current code in my first post in this topic and nothing changed.

                So now you're instantiating the class two times? One in the ui and one in the ctor? How should this ever work?

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

                clostridium_difficileC 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @clostridium_difficile said in QWidget subclass does not appear on main window.:

                  I showed you my current code in my first post in this topic and nothing changed.

                  So now you're instantiating the class two times? One in the ui and one in the ctor? How should this ever work?

                  clostridium_difficileC Offline
                  clostridium_difficileC Offline
                  clostridium_difficile
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher it it possible to have multiple instancens of the same widget type. I know what you mean – I didn't deleted ToolBar * toolBar; in Mainwindow – don't you? Apparently, it works for some reason – had two instances of this widget. I deleted it now, with initialization in constructor, but left only one instance, and nothing more has changed.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    So now you can access your toolbar with 'ui->toolbar'? Please reimplement the paint event as suggested by @mrjj and fill your widget with e.g. a red color.

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

                    clostridium_difficileC 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      So now you can access your toolbar with 'ui->toolbar'? Please reimplement the paint event as suggested by @mrjj and fill your widget with e.g. a red color.

                      clostridium_difficileC Offline
                      clostridium_difficileC Offline
                      clostridium_difficile
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher I've did it already. I see frame. Ok, thats what I wanted to achieve, but why setStyleSheet does not work?

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @clostridium_difficile said in QWidget subclass does not appear on main window.:

                        but why setStyleSheet does not work?

                        I would guess because autoFillBackground is not set to true.

                        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
                        0

                        • Login

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