Qt 동적으로 컨트롤 추가 및 지정 컨트롤 삭제
4766 단어 QT
실현 가능한 기능
1. 흩어진 컨트롤러를 하나의 모듈로 포장하여 전체 모듈을 추가한다.2. 각 모듈의 단독 삭제를 실현한다.3. 수치, 기능의 상호작용 기능을 실현한다.버튼 등.
코드 내용
말을 많이 하지 말고 바로 코드를 붙여라.손수 쓴 것이기 때문에 주석이 많지 않으니 스스로 깨닫고 공사 서류를 문말에 놓아라.mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
QGridLayout *m_gLayout;
QListmtestForms;
int count;
void delete_Layout();
private slots:
void on_add_btn_clicked();
void mdel_btn_clicked();
private:
Ui::MainWindow *ui;
signals:
void add_info(int num);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_gLayout=new QGridLayout;
ui->widget->setLayout(m_gLayout);
count=0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_add_btn_clicked()
{
if(ui->lineEdit->text()!=""){
count=mtestForms.size();
testForm *mtestForm=new testForm();
mtestForms.append(mtestForm);
mtestForm->setEdit(ui->lineEdit->text());
connect(mtestForm,SIGNAL(mdel()),this,SLOT(mdel_btn_clicked()));
m_gLayout->addWidget(mtestForm);
}else{
QMessageBox::information(this,"warming"," ");
}
}
void MainWindow::mdel_btn_clicked()
{
testForm *tf = qobject_cast(sender()); //
m_gLayout->removeWidget(tf);
tf->deleteLater();
}
testfom.h
#ifndef TESTFORM_H
#define TESTFORM_H
#include
#include
#include
#include
#include
#include
#include
#include
class testForm:public QWidget
{
Q_OBJECT
public:
testForm();
QPushButton *m_btn_start;
QPushButton *m_btn_delete;
QLabel *m_label_Name;
QLabel *m_label_Count;
QLineEdit *m_lineEidt;
QHBoxLayout *m_hlayoutLabel;
QTimer *ask_COM_Timer;
void setEdit(QString type);
int count;
private slots:
void del_btn_click();
void startTimer();
void printcount();
signals:
void mdel();
};
#endif // TESTFORM_H
testfom.cpp
#include "testform.h"
testForm::testForm()
{
m_btn_start=new QPushButton();
connect(m_btn_start,SIGNAL(clicked()),this,SLOT(startTimer()));
m_btn_start->setText(" ");
m_btn_delete=new QPushButton();
connect(m_btn_delete,SIGNAL(clicked()),this,SLOT(del_btn_click()));
m_btn_delete->setText(" ");
ask_COM_Timer=new QTimer(this);
ask_COM_Timer->setInterval(1000);
connect(ask_COM_Timer,SIGNAL(timeout()),this,SLOT(printcount()));
m_label_Name=new QLabel();
m_label_Count=new QLabel();
m_lineEidt=new QLineEdit();
m_hlayoutLabel=new QHBoxLayout;
m_hlayoutLabel->addWidget(m_btn_start,0,nullptr);
m_hlayoutLabel->addWidget(m_lineEidt,1,nullptr);
m_hlayoutLabel->addWidget(m_label_Name,2,nullptr);
m_hlayoutLabel->addWidget(m_label_Count,3,nullptr);
m_hlayoutLabel->addStretch(4);
m_hlayoutLabel->addWidget(m_btn_delete,5,nullptr);
this->setLayout(m_hlayoutLabel);
QSizePolicy policy = this->sizePolicy();
policy.setHorizontalPolicy(QSizePolicy::Preferred);
policy.setVerticalPolicy(QSizePolicy::Fixed);
this->setSizePolicy(policy);
}
void testForm::del_btn_click()
{
emit mdel();
}
void testForm::setEdit(QString type)
{
m_lineEidt->setText(type);
m_label_Name->setText(type);
}
void testForm::startTimer()
{
count=0;
if(m_btn_start->text()==" "){
ask_COM_Timer->start();
m_btn_start->setText(" ");
}else if(m_btn_start->text()==" "){
ask_COM_Timer->stop();
m_btn_start->setText(" ");
}
}
void testForm::printcount(){
qDebug()<text()+" "+QString::number(count);
m_label_Count->setText(QString::number(count));
count++;
}
프로젝트 파일 다운로드:동적 증감 컨트롤 코드
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
qt 초급 오류: 헤더 파일에서 정의, cpp가 실행되지 않았습니다.문제 :-1: error: symbol(s) not found for architecture x86_64 :-1: error: linker command failed with exit code 1 (use -v to...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.