I am working in Qt in my program. I have a widget window and I have placed a button and a list box in it. Then I have made a class called myplot which graph plotting is done. Then in my button event handler I have called the object of myplot
myplot * p1 = new myplot(session,session ,24, "session"); // send arrays in argument containing the data to be plot
p1->show();
myplot * p2 = new myplot(payload,payload ,24, "payload"); // send arrays in argument containing the data to be plot
p2->show();
It is working fine as my graph appear in new window, but what I want is that graphs should appear in my mainwidget window.
What I did next was to remove the title bar of my graphs window I wrote this Qt Code:
p1->setWindowFlags(Qt::FramelessWindowHint);
p2->setWindowFlags(Qt::FramelessWindowHint);
Now
1) what should I do to place and append the graphs window in my main window? Also when I close main window my graph window should close.
2) when I select an other value from the list box and click button 开发者_StackOverflow社区my old graph disappear and new should appeari drag and droped a verticlalayout then write
ui->verticalLayout->addChildWidget(p1);
but got an error
/usr/include/qt4/QtGui/qlayout.h:191: error: ‘void QLayout::addChildWidget(QWidget*)’ is protected
then i draaged and droped a scrollarea and wrote
myplot * p1 = new myplot(session,session ,24, "session");
p1->setWindowFlags(Qt::FramelessWindowHint);
ui->scrollArea->addScrollBarWidget(p1,0);
p1->show();
my graphs stopped appearing kindly guide me am i doing it wrong or what the right way
Apparently, myplot
is a childclass of QWidget. So you can just define a layout on your main window (e.g. using QHBoxLayout
), and add those two widgets to it. This will cause them to be drawn inside the mainwindow. Also make sure that you pass the main window as the QWidget parent of the plots.
For replacing the plots, you can either remove the old plots from the layout and add new ones. But I would prefer to make the plots members of your main window and include some update method that would redraw with the new data
When you call show
on widgets with no parent like that, it automatically gets wrapped in a window. What you should do is create one widget and add the graphs to it's layout. See QMainWindow examples, and section about dock widgets if that's what you want.
Basically you don't need to worry about the window they are appearing in, it is only appearing because they have no other parent.
What you need to do is to specify a parent widget when you construct them.
(This answer is based on the assumption that your plots are also QWidgets, if you could mention what class they actually are and perhaps link to the documentation it would really help us to answer you.)
精彩评论