Implementation features: Add several components by clicking the button.
Note: At first, I did not use the layout manager, so no matter how you clicked, the added components could not be displayed. When the layout manager is added, the components are displayed properly, indicating that adding and removing components requires the layout manager's participation.
(Correction: The original dynamic increase of the build does not necessarily require the participation of the layout manager, the original test when the component is not displayed, it is because I did not use the show () method caused by, hereby corrected. ---(rookie works, mistakes are difficult to avoid, only reference value)
The code is as follows:
#ifndef mymainframe_h_
#define Mymainframe_h_
#include <QtGui/QWidget>
#include <qtgui/ qlineedit>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
class Mymainframe : Public qwidget
{
q_object public
:
mymainframe ();
~mymainframe ();
Private:
Qpushbutton *pb1;
Qwidget *qw;
Qvboxlayout *QB;
Public slots:
void Showstation ();
};
#endif
#include "MyMainFrame.h"
#include <iostream>
mymainframe::mymainframe ()
{
setgeometry ( 0,0,300,200);
Pb1=new Qpushbutton ("Click it and list would be shown", this);
std::cout<< "Construction Method:" <<this<< "\ n";
Pb1->setgeometry (20,10,260,40);
Qw=new Qwidget (this);
Qw->setgeometry (40,50,200,90);
Qb=new qvboxlayout (QW);
Connect (pb1,signal (clicked ()), This,slot (Showstation ()));
}
void Mymainframe::showstation () {
Qpushbutton *pb2=new Qpushbutton ("first");
Qb->addwidget (PB2);
std::cout<< "Method Inside:" <<this<< "\ n";
Qpushbutton *pb3=new Qpushbutton ("second");
Qb->addwidget (PB3);
Pb1->settext ("Hello");
Pb2->setgeometry (0,0,200,30);
Pb3->setgeometry (0,0,200,30);
This->update (0,0,300,200);
}
Mymainframe::~mymainframe ()
{
}
#include <QtGui/QApplication>
#include "MyMainFrame.h"
int main (int argc,char *argv[])
{
Qapplication A (ARGC,ARGV);
Mymainframe *my=new mymainframe ();
My->show ();
return a.exec ();
}
Run the screenshot as follows:
Before clicking:
After clicking:
(-------------finished------------)