3 widgets in only one central widget
-
Hi all,
I would like to show in the central widget of mine MainWindow.ui 3 Widgets (1 QCustomplot and 2 QLed) but my program, as i wrote, gives me the possibility to show in the central widget only the first.QLed *led_1 = new QLed; QLed *led_2 = new QLed; QHBoxLayout *mainLayout = new QHBoxLayout; QVBoxLayout *layout = new QVBoxLayout; mainLayout->addWidget(m_plot); //mainLayout->addWidget(layout); layout->addWidget(led_1); layout->addWidget(led_2); setCentralWidget(m_plot); //setCentralWidget(m_luce); [...] m_plot->addGraph(); m_plot->graph(0)->setPen(QPen(Qt::blue)); // give the axes some labels: m_plot->xAxis->setLabel("Time (sec.)"); m_plot->yAxis->setLabel("Range (mm)"); QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime); timeTicker->setTimeFormat("%s"); m_plot->xAxis->setTicker(timeTicker); m_plot->axisRect()->setupFullAxesBox(); m_plot->yAxis->setRange(0, 2200); // configure right and top axis to show ticks and labels: m_plot->xAxis2->setVisible(true); m_plot->xAxis2->setTickLabels(false); m_plot->yAxis2->setVisible(true); m_plot->yAxis2->setTickLabels(false); [...]
This is the mainwondow.ui . In the centralWidget there are plot on the left, and 2 leds on the right (1 on the top right corner and the other on the bottom right).
Any suggestion?
I tried to use only mainwindow.ui to solve this problem but at the end the graph did not receive the data to plot. -
Hi all,
I would like to show in the central widget of mine MainWindow.ui 3 Widgets (1 QCustomplot and 2 QLed) but my program, as i wrote, gives me the possibility to show in the central widget only the first.QLed *led_1 = new QLed; QLed *led_2 = new QLed; QHBoxLayout *mainLayout = new QHBoxLayout; QVBoxLayout *layout = new QVBoxLayout; mainLayout->addWidget(m_plot); //mainLayout->addWidget(layout); layout->addWidget(led_1); layout->addWidget(led_2); setCentralWidget(m_plot); //setCentralWidget(m_luce); [...] m_plot->addGraph(); m_plot->graph(0)->setPen(QPen(Qt::blue)); // give the axes some labels: m_plot->xAxis->setLabel("Time (sec.)"); m_plot->yAxis->setLabel("Range (mm)"); QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime); timeTicker->setTimeFormat("%s"); m_plot->xAxis->setTicker(timeTicker); m_plot->axisRect()->setupFullAxesBox(); m_plot->yAxis->setRange(0, 2200); // configure right and top axis to show ticks and labels: m_plot->xAxis2->setVisible(true); m_plot->xAxis2->setTickLabels(false); m_plot->yAxis2->setVisible(true); m_plot->yAxis2->setTickLabels(false); [...]
This is the mainwondow.ui . In the centralWidget there are plot on the left, and 2 leds on the right (1 on the top right corner and the other on the bottom right).
Any suggestion?
I tried to use only mainwindow.ui to solve this problem but at the end the graph did not receive the data to plot.@venale17 said in 3 widgets in only one central widget:
setCentralWidget(m_plot);
If m_plot is your central widget then the other two needs to be children of it. Or use another widget as central widget where you put all 3 widget.
-
Hi jsum, can you give me some example? Because I don't understand.
You think that i should create a pointer like QLed *led and then transforms it in a "children"? Sorry but i don't know how to do it...QLed *led_1 = new QLed(m_plot); QLed *led_2 = new QLed(m_plot);
Or, if you want to use a common widget as central widget:
QWidget *centralWidget = new QWidget(); m_plot = new WHAT_EVER_IT_IS(centralWidget); QLed *led_1 = new QLed(centralWidget); QLed *led_2 = new QLed(centralWidget); // Set up layouts here setCentralWidget(centralWidget);
In the code you posted your leds are not put on any widgets and the layout you're using for them is not set on any widget or other layout.
-
QLed *led_1 = new QLed(m_plot); QLed *led_2 = new QLed(m_plot);
Or, if you want to use a common widget as central widget:
QWidget *centralWidget = new QWidget(); m_plot = new WHAT_EVER_IT_IS(centralWidget); QLed *led_1 = new QLed(centralWidget); QLed *led_2 = new QLed(centralWidget); // Set up layouts here setCentralWidget(centralWidget);
In the code you posted your leds are not put on any widgets and the layout you're using for them is not set on any widget or other layout.
-
Hi,
Use the layouts you had before and put the main layout in the container widget.
-
Hi @SGaist,
sorry but i don't understand what you mean with "container widget".
I cannot write setCentralWidget(mainwidget) because qt give me an error@venale17 said in 3 widgets in only one central widget:
"container widget".
Hi
that is just a term for the widget holding it all.
From @jsulm code, its the centralWidgetQWidget *centralWidget = new QWidget(); // this is our "container widget" m_plot = new QCustomPlot(centralWidget); QLed *led_1 = new QLed(centralWidget); QLed *led_2 = new QLed(centralWidget); // Set up layouts here setCentralWidget(centralWidget);
what error do you get from this code ?
-
Ok, maybe i understand:
i wrote this:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_plot(new QCustomPlot), m_ui(new Ui::MainWindow), m_luce(new Luce(m_plot)), m_status(new QLabel), m_console(new Console), m_settings(new SettingsDialog), //! [1] m_serial(new QSerialPort(this)) //! [1] { //! [0] m_ui->setupUi(this); m_console->setEnabled(false); QWidget *centralWidget = new QWidget(); // this is our "container widget" m_plot = new QCustomPlot(centralWidget); m_luce = new Luce(centralWidget); // Set up layouts here QHBoxLayout *mainlayout = new QHBoxLayout(centralWidget); mainlayout->addWidget(m_plot); mainlayout->addWidget(m_luce); setCentralWidget(centralWidget); [...]
I create a pointer m_luce that describes the features of the leds and, with your examples i reach a good solution:
-
Hi
Super.
But you create them twice which is not needed.you have
m_plot(new QCustomPlot), // you new them here
m_luce(new Luce(m_plot)),so you can just do
QWidget *centralWidget = new QWidget(); // this is our "container widget"
m_plot = new QCustomPlot(centralWidget);we have new them already
m_luce = new Luce(centralWidget);// Set up layouts here QHBoxLayout *mainlayout = new QHBoxLayout(centralWidget); mainlayout->addWidget(m_plot); mainlayout->addWidget(m_luce);
-
Hi all,
I have the same problem again.
I don't change anything of the code, just the PC.
I reinstalled Qt and i noticed that:and this, is the code that i wrote, with you're helps:
m_ui->setupUi(this); m_console->setEnabled(false); QWidget *centralWidget = new QWidget(); // creo un container widget che raccolga i widget che creerò // Set up layouts here QHBoxLayout *mainlayout = new QHBoxLayout(centralWidget); //imposto un layout orizzontale mainlayout->addWidget(m_plot); mainlayout->addWidget(m_luce); mainlayout->addStretch(); //aggiungo l'oggetto puntato da m_plot e m_luce all'interfaccia secondo il layout orizzontale setCentralWidget(centralWidget); setCentralWidget(centralWidget);
Someone have any idea? How can i divide the central widget in two equal part, one for the plot and the other for the leds?
-
Hi all,
I have the same problem again.
I don't change anything of the code, just the PC.
I reinstalled Qt and i noticed that:and this, is the code that i wrote, with you're helps:
m_ui->setupUi(this); m_console->setEnabled(false); QWidget *centralWidget = new QWidget(); // creo un container widget che raccolga i widget che creerò // Set up layouts here QHBoxLayout *mainlayout = new QHBoxLayout(centralWidget); //imposto un layout orizzontale mainlayout->addWidget(m_plot); mainlayout->addWidget(m_luce); mainlayout->addStretch(); //aggiungo l'oggetto puntato da m_plot e m_luce all'interfaccia secondo il layout orizzontale setCentralWidget(centralWidget); setCentralWidget(centralWidget);
Someone have any idea? How can i divide the central widget in two equal part, one for the plot and the other for the leds?