Beginner QT--dynamic addition of components.

Source: Internet
Author: User

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------------)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.