Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

If I want to use "setCentralWidget" in Widget, what should I do.



  • I am now porting the sample code of QCustomPlot. The sample code of QCustomPlot uses QMainWindow, and I want to use QWidget. What should I do?

    this is my code

    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget),
          mPlot(0),
          mTag1(0),
          mTag2(0)
    {
        ui->setupUi(this);
    
        mPlot = new QCustomPlot(this);
        setCentralWidget(mPlot);
    }
    

    this is QCustomPlot`s sample code

    MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow),
      mPlot(0),
      mTag1(0),
      mTag2(0)
    {
      ui->setupUi(this);
      
      mPlot = new QCustomPlot(this);
      setCentralWidget(mPlot);
    }
    

    Please help me!😭😭



  • You can only use setCentralWidget with a QMainWindow. Instead use QWidget::setLayout to add a layout and then add a QWidget to the layout.

    See the documentation for QWidget::setLayout.



  • The QWidget is a separate ‘.ui’ file.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The answer of @skycrestway still holds. Your widget needs a layout. After that you can add your plot to that layout.



  • @SGaist oh!i know,thanks!


Log in to reply