PDA

View Full Version : How to take window from QWorkspace?



8Observer8
22nd August 2013, 12:31
Hi,

I try to take window from QWorkspace. But it doesn't work:


m_tablePanel = (TabelPanel *)(workspace->windowList()).at(0);

Thank you!

wysota
22nd August 2013, 12:33
Please define what you mean by "it doesn't work".

8Observer8
22nd August 2013, 12:40
I cannot update my Plot.

9452

I'm a little ashamed for my code. But this is my first MDI program.

There is a little code:



m_tablePanel = (TabelPanel *)(workspace->windowList()).at(0);
m_plotPanel = (PlotPanel *)(workspace->windowList()).at(1);

connect(m_tablePanel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(updatePanel(QStandardItem*)));




void MainWindow::updatePanel(QStandardItem *item)
{
m_plotPanel->updatePanel(model);
}

wysota
22nd August 2013, 12:54
Maybe you can't update your plot but it doesn't mean the method doesn't work. Especially that you have an ugly C cast instead of qobject_cast or something similar. How do you know that the first item in the list is an instance of TablePanel and the second one is an instance of PlotPanel?

8Observer8
22nd August 2013, 13:13
I want to take instance of TablePanel and PlotPanel from workspace. I don't know how to do it.

It's doesn't work:



m_tablePanel = qobject_cast<TabelPanel *>((workspace->windowList()).at(0));
m_plotPanel = qobject_cast<PlotPanel *>((workspace->windowList()).at(1));



How do you know that the first item in the list is an instance of TablePanel and the second one is an instance of PlotPanel?

I added the Table then the Plot.



workspace->addWindow(tablePanel);
workspace->addWindow(plotPanel);


Slot of MainWindow updatePanel() doesn't work.


connect(m_tablePanel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(updatePanel(QStandardItem*)));

wysota
22nd August 2013, 14:27
I want to take instance of TablePanel and PlotPanel from workspace. I don't know how to do it.

It's doesn't work:



m_tablePanel = qobject_cast<TabelPanel *>((workspace->windowList()).at(0));
m_plotPanel = qobject_cast<PlotPanel *>((workspace->windowList()).at(1));

Define "doesn't work". Does the program crash or do you get a result different than what you expected to get?


Slot of MainWindow updatePanel() doesn't work.
The slot doesn't get called or it behaves differently to what you expected?

8Observer8
22nd August 2013, 14:48
I set a breakpoint in updatePanel() SLOT


void MainWindow::updatePanel(QStandardItem *item)
{
m_plotPanel->updatePanel(model);
}

wysota
22nd August 2013, 15:26
You set a breakpoint and?

8Observer8
22nd August 2013, 15:29
You set a breakpoint and?

Then I change value in the table. But nothing happens.

wysota
22nd August 2013, 16:30
Apparently your signal-slot connection is invalid.

TablePanel class is unlikely to have a signal itemChanged() carrying a QStandardItem pointer unless you added such signal to the class yourself.

8Observer8
22nd August 2013, 16:42
Thanks! Now the breakpoint works.



connect(model, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(updatePanel(QStandardItem*)));


But the Plot is not redrawn.

plotpanel.cpp


#include "plotpanel.h"
#include <QHBoxLayout>
#include <QDebug>
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>

PlotPanel::PlotPanel(QWidget *parent) :
QDialog(parent)
{
plot = new QwtPlot;

QHBoxLayout *mainLayout = new QHBoxLayout;

mainLayout->addWidget(plot);

this->setLayout(mainLayout);
}

void PlotPanel::updatePanel(QStandardItemModel *model)
{
plot->setAxisScale(QwtPlot::yLeft, 0.0, 10);
plot->setAxisScale(QwtPlot::xBottom, 0.0, 10);

QwtPlotCurve *curve = new QwtPlotCurve;
curve->setTitle(tr("Points"));
curve->setPen(QPen(Qt::blue, 4));
curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);

QwtSymbol *symbol = new QwtSymbol(QwtSymbol::Ellipse, QBrush(Qt::yellow),
QPen(Qt::red, 2), QSize(8, 8));
curve->setSymbol(symbol);

QPolygonF points;

double x, y;
for (int row = 0; row < model->rowCount(); row++) {
QModelIndex index = model->index(row, 0);
x = model->data(index).toDouble();

index = model->index(row, 1);
y = model->data(index).toDouble();

points << QPointF(x, y);
}

curve->setSamples(points);
curve->attach(plot);

// this->update();
}

wysota
22nd August 2013, 17:28
I don't see you calling replot() on the plot anywhere.

8Observer8
22nd August 2013, 18:05
Thanks! Thanks! Thanks! :)

There is an another problem. I wanna see the new plot. How to delete the old plot?

9458

wysota
22nd August 2013, 18:11
Why not reuse the old curve instead?

8Observer8
22nd August 2013, 18:24
Maybe I must delete the old curve? How to do it? I don't understand...

wysota
22nd August 2013, 18:27
If you want to remove the old curve then detach it from the plot and delete it.

8Observer8
22nd August 2013, 18:42
How to get the old curve?

wysota
22nd August 2013, 20:21
You can store it in some member variable and then retrieve it later when you need it.

8Observer8
23rd August 2013, 02:59
Nice! It works! Thank you much :)

9459

This program is still left unfinished. But it may be useful for someone.

8Observer8
23rd August 2013, 05:08
I made a loading (saving) table from (to) a xml-file:

Table.xml


<Table nrows="5" ncols="2">
<point x="0.02" y="0.33"/>
<point x="0.56" y="1.12"/>
<point x="2.22" y="2.33"/>
<point x="6.23" y="4.87"/>
<point x="9.55" y="8.23"/>
</Table>


9461

8Observer8
23rd August 2013, 12:39
I replace QWorkspace -> QMdiArea



//workspace = new QWorkspace;
mdiArea = new QMdiArea;
//setCentralWidget( workspace );
setCentralWidget( mdiArea );

createActions();
createMenus();
createToolbars();

showScenePanel();
showControlPanel();

scenePanel = new ScenePanel;
//scenePanel = qobject_cast<ScenePanel *>((workspace->windowList()).at(0));
scenePanel = qobject_cast<ScenePanel *>((mdiArea->subWindowList()).at(0));
controlPanel = new ControlPanel;
//controlPanel = qobject_cast<ControlPanel *>((workspace->windowList()).at(1));
controlPanel = qobject_cast<ControlPanel *>((mdiArea->subWindowList()).at(1));

scenePanel->setWindowIcon(QIcon(":/Icons/Scene.png")); // <---- RED LINE
controlPanel->setWindowIcon(QIcon(":/Icons/Control.png"));



But the program crashes at run time (on the red line)

Santosh Reddy
23rd August 2013, 12:54
Comment lines #15 and #18 in the above code

8Observer8
23rd August 2013, 13:22
I did it. The program isn't crushed.

But I see:

9467

My Project:

Santosh Reddy
23rd August 2013, 13:25
Here is corrected code...

wysota
23rd August 2013, 13:31
QList<QMdiSubWindow*> windows = mdi->subWindowList();
windows.at(0)->setWindowIcon(QIcon(":/Icons/Scene.png"));
windows.at(1)->setWindowIcon(QIcon(":/Icons/Control.png"));

8Observer8
23rd August 2013, 14:30
I have ran it with "QWorkspase". It worked normally.



QList<QMdiSubWindow*> windows = mdi->subWindowList();
windows.at(0)->setWindowIcon(QIcon(":/Icons/Scene.png"));
windows.at(1)->setWindowIcon(QIcon(":/Icons/Control.png"));

Thanks! It works.

But program works illegal.

Added after 5 minutes:

How to do that:



scenePanel = qobject_cast<ScenePanel *>(windows.at(0));
controlPanel = qobject_cast<ControlPanel *>(windows.at(1));



Here is corrected code...

Thank you very much :)

Added after 40 minutes:

In addition, instead:


scenePanel->resize(500, 500);

I wrote


sceneWindow->resize(500, 500);

Guys, thanks for help! I am happy :D

wysota
23rd August 2013, 14:41
How to do that:



scenePanel = qobject_cast<ScenePanel *>(windows.at(0));
controlPanel = qobject_cast<ControlPanel *>(windows.at(1));

This won't work because the returned widget is an instance of QMdiSubWindow and not your widget.

8Observer8
23rd August 2013, 14:48
Santosh Reddy have corrected:

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "controlpanel.h"
#include "scenepanel.h"

class QAction;
class QMdiSubWindow;
class QMdiArea;
class QMenu;

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);

signals:

public slots:

private:
void createActions();
void createMenus();
void createToolbars();

QMdiArea *mdiArea;

QAction *sceneAction;
QAction *controlAction;

ScenePanel *scenePanel;
ControlPanel *controlPanel;

QMdiSubWindow * sceneWindow;
QMdiSubWindow * controlWindow;

private slots:
void showScenePanel();
void showControlPanel();
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include <QtGui>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
this->setWindowTitle(tr("mdiLine"));
mdiArea = new QMdiArea;
setCentralWidget( mdiArea );

createActions();
createMenus();
createToolbars();

scenePanel = new ScenePanel;
scenePanel->setWindowIcon(QIcon(":/Icons/Scene.png"));
sceneWindow = mdiArea->addSubWindow(scenePanel);
sceneWindow->resize(500, 500);

controlPanel = new ControlPanel;
controlPanel->setWindowIcon(QIcon(":/Icons/Control.png"));
controlWindow = mdiArea->addSubWindow(controlPanel);

connect(controlPanel, SIGNAL(drawLine(QVector3D,QVector3D)), scenePanel, SLOT(drawLine(QVector3D,QVector3D)));

showScenePanel();
showControlPanel();
}

void MainWindow::createActions() {
sceneAction = new QAction( QIcon(":/Icons/Scene.png"), tr("&Scene"), this );
sceneAction->setStatusTip( tr("Scene Panel") );
connect( sceneAction, SIGNAL(triggered()), this, SLOT(showScenePanel()) );

controlAction = new QAction( QIcon(":/Icons/Control.png"), tr("&Control"), this );
controlAction->setStatusTip( tr("Control Panel") );
connect( controlAction, SIGNAL(triggered()), this, SLOT(showControlPanel()) );
}

void MainWindow::createMenus() {
QMenu *menu;
menu = menuBar()->addMenu( tr("&Panels") );
menu->addAction( sceneAction );
menu->addAction( controlAction );
}

void MainWindow::createToolbars() {
QToolBar *toolbar;

toolbar = addToolBar( tr("Panels") );
toolbar->addAction( sceneAction );
toolbar->addAction( controlAction );
}

void MainWindow::showScenePanel()
{
sceneWindow->show();
}

void MainWindow::showControlPanel()
{
controlWindow->show();
}