最近写一个弹出菜单,使用的是QT5.12.12,
效果展示图:
头文件如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QGroupBox>
#include <QComboBox>
#include <QRadioButton>
#include <QLabel>
#include <QVector>
#include <QStringList>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <vector>
#include <QButtonGroup>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class OutPort
{
public:
void addViaComboBox();
void addVetComboBox();
};
class ModePattern: public QGroupBox
{
Q_OBJECT
public:
explicit ModePattern(const QString &str = nullptr);
~ModePattern();
void addPattern(const QString &str);
signals:
void outString(const QStringList & str);
private:
QString name_;
QStringList emit_data;
QStringList * pattern_comboBox_list_;
QStringList * data_comboBox_list_; //未使用
QVBoxLayout * main_vbox;
QLabel *label_1;
QLabel *label_2;
QLabel *label_3_0;
QLabel *label_3_1;
QComboBox * pattern_comboBox_;
// QComboBox * oterComboBox_;
QButtonGroup * radioButton_group;
QRadioButton *radioButton_1;
QRadioButton *radioButton_2;
QRadioButton *radioButton_3;
std::vector<QVBoxLayout *> vbox_vector;
std::vector<QHBoxLayout *> hbox_vector;
std::vector<QLabel *> label_vector;
void newWidget();
void setWidgetStyle();
void setWidgetLayout();
};
class ModeLine: public QGroupBox
{
Q_OBJECT
public:
explicit ModeLine(const QString &str = nullptr);
~ModeLine();
signals:
void outString(const QStringList & str);
private:
QString name_;
QStringList emit_data;
// QStringList * comboBox_1_0_string_list_;
// QStringList * comboBox_1_1_string_list_;
// QStringList * comboBox_2_0_string_list_;
// QStringList * comboBox_2_1_string_list_;
QVBoxLayout *main_vbox;
QLabel * label_1;
QLabel * label_2;
QLabel * label_3;
QComboBox * comboBox_1_0;
QComboBox * comboBox_1_1;
QComboBox * comboBox_2_0;
QComboBox * comboBox_2_1;
void newWidget();
void setWidgetStyle();
void setWidgetLayout();
};
class ModeData: public QGroupBox
{
Q_OBJECT
public:
explicit ModeData(const QString &str = nullptr);
~ModeData();
private:
void newWidget();
void setWidgetStyle();
void setWidgetLayout();
QString name_;
};
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void addViaComboBox();
void addVetComboBox();
private:
int32_t label_normall_width;
int32_t label_normall_heigth;
int32_t label_icon_width;
int32_t label_icon_heigth;
int32_t comboBox_normall_width;
int32_t comboBox_normall_heigth;
QVBoxLayout * main_vbox; //totul layou
QComboBox * viaComboBox_;
QComboBox * vetComboBox_;
QLabel * viaLable_;
QLabel * viaLable_icon_;
QLabel * vetLable_;
QLabel * vetLable_icon_;
std::vector<QVBoxLayout *> vbox_vector;
std::vector<QHBoxLayout *> hbox_vector;
std::vector<QLabel *> label_vector;
// son class
ModePattern * pattern;
ModeLine * line;
ModeData * end_modue;
void initData();
void newWidget();
void setWidgetStyle();
void setWidgetLayout();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
cpp文件如下
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(330,460);
// this->setFixedSize(330,460);
initData();
newWidget();
setWidgetStyle();
setWidgetLayout();
}
void Widget::initData()
{
label_normall_width = 60;
label_normall_heigth = 22;
label_icon_width = 15;
label_icon_heigth = 15;
comboBox_normall_width = 220;
comboBox_normall_heigth = 22;
}
void Widget::newWidget()
{
//son class
pattern = new ModePattern("Pattern");
line = new ModeLine("Line");
end_modue = new ModeData("data");
//my class data
viaComboBox_ = new QComboBox(this);
vetComboBox_ = new QComboBox(this);
viaLable_ = new QLabel("Via",this);
viaLable_icon_ = new QLabel("",this);
vetLable_ = new QLabel("Vet",this);
vetLable_icon_ = new QLabel("",this);
label_vector.emplace_back(viaLable_);
label_vector.emplace_back(viaLable_icon_);
label_vector.emplace_back(vetLable_);
label_vector.emplace_back(vetLable_icon_);
}
void Widget::setWidgetStyle()
{
viaLable_->setFixedSize(label_normall_width,label_normall_heigth);
viaLable_icon_->setFixedSize(label_icon_width,label_icon_heigth);
vetLable_->setFixedSize(label_normall_width,label_normall_heigth);
vetLable_icon_->setFixedSize(label_icon_width,label_icon_heigth);
}
void Widget::setWidgetLayout()
{
main_vbox = new QVBoxLayout(this);
QHBoxLayout * via_hbox = new QHBoxLayout(this);
QHBoxLayout * vet_hbox = new QHBoxLayout(this);
hbox_vector.emplace_back(via_hbox);
hbox_vector.emplace_back(vet_hbox);
via_hbox->addWidget(viaLable_);
via_hbox->addWidget(viaLable_icon_);
via_hbox->addWidget(viaComboBox_);
vet_hbox->addWidget(vetLable_);
vet_hbox->addWidget(vetLable_icon_);
vet_hbox->addWidget(vetComboBox_);
main_vbox->addLayout(via_hbox);
main_vbox->addLayout(vet_hbox);
main_vbox->addWidget(pattern);
main_vbox->addWidget(line);
main_vbox->addWidget(end_modue);
this->setLayout(main_vbox);
}
void Widget::addViaComboBox()
{
}
void Widget::addVetComboBox()
{
}
Widget::~Widget()
{
delete ui;
delete pattern;
delete line;
delete end_modue;
}
//-------------------------------------------
ModePattern::ModePattern(const QString &str):name_(str)
{
this->setTitle(str);
newWidget();
setWidgetStyle();
setWidgetLayout();
}
void ModePattern::newWidget()
{
pattern_comboBox_list_ = new QStringList;
main_vbox = new QVBoxLayout;
pattern_comboBox_ = new QComboBox(this);
// oterComboBox_ = new QComboBox(this);
radioButton_1 = new QRadioButton("pattern",this);
radioButton_2 = new QRadioButton("",this);
radioButton_3 = new QRadioButton("",this);
label_1 = new QLabel("1",this);
label_2 = new QLabel("1",this);
label_3_0 = new QLabel("1",this);
label_3_1 = new QLabel("1",this);
label_vector.emplace_back(label_1);
label_vector.emplace_back(label_2);
label_vector.emplace_back(label_3_0);
label_vector.emplace_back(label_3_1);
connect(pattern_comboBox_,&QComboBox::currentTextChanged,
[=](const QString &str)
{
emit_data.append(str);
emit outString(emit_data);
}
);
}
void ModePattern::setWidgetStyle()
{
}
void ModePattern::setWidgetLayout()
{
QHBoxLayout *hbox_1 = new QHBoxLayout();
QHBoxLayout *hbox_2 = new QHBoxLayout();
QHBoxLayout *hbox_3 = new QHBoxLayout();
QVBoxLayout *vbox_radio = new QVBoxLayout();
hbox_1->addWidget(label_1);
hbox_1->addWidget(pattern_comboBox_);
vbox_radio->addWidget(radioButton_1);
vbox_radio->addWidget(radioButton_2);
vbox_radio->addWidget(radioButton_3);
hbox_2->addWidget(label_2);
hbox_2->addLayout(vbox_radio);
hbox_3->addWidget(label_3_0);
hbox_3->addWidget(label_3_1);
main_vbox->addLayout(hbox_1);
main_vbox->addLayout(hbox_2);
main_vbox->addLayout(hbox_3);
this->setLayout(main_vbox);
}
void ModePattern::addPattern(const QString &str)
{
pattern_comboBox_list_->append(str);
pattern_comboBox_->addItem(str);
}
ModePattern::~ModePattern()
{
delete pattern_comboBox_list_;
}
//-------------------------------------------
ModeLine::ModeLine(const QString &str ):name_(str)
{
this->setTitle(str);
newWidget();
setWidgetStyle();
setWidgetLayout();
}
void ModeLine::newWidget()
{
emit_data.reserve(4); //扩容四个空间
main_vbox = new QVBoxLayout;
label_1 = new QLabel("1",this);
label_2 = new QLabel("2",this);
label_3 = new QLabel("3",this);
comboBox_1_0 = new QComboBox(this);
comboBox_1_1 = new QComboBox(this);
comboBox_2_0 = new QComboBox(this);
comboBox_2_1 = new QComboBox(this);
connect(comboBox_1_0,QComboBox::currentTextChanged,
[&](const QString &str)
{
emit_data[0] = str;
qDebug() << str;
}
);
connect(comboBox_1_1,QComboBox::currentTextChanged,
[&](const QString &str)
{
emit_data[1] = str;
qDebug() << str;
}
);
connect(comboBox_2_0,QComboBox::currentTextChanged,
[&](const QString &str)
{
emit_data[0] = str;
qDebug() << str;
}
);
connect(comboBox_2_1,QComboBox::currentTextChanged,
[&](const QString &str)
{
emit_data[0] = str;
qDebug() << str;
}
);
}
void ModeLine::setWidgetStyle()
{
}
void ModeLine::setWidgetLayout()
{
QHBoxLayout *hbox_1 = new QHBoxLayout();
QHBoxLayout *hbox_2 = new QHBoxLayout();
QHBoxLayout *hbox_3 = new QHBoxLayout();
QHBoxLayout *hbox_4 = new QHBoxLayout();
hbox_1->addWidget(label_1);
hbox_2->addWidget(label_2);
hbox_2->addWidget(comboBox_1_0);
hbox_2->addWidget(comboBox_1_1);
hbox_3->addWidget(label_3);
hbox_3->addWidget(comboBox_2_0);
hbox_3->addWidget(comboBox_2_1);
main_vbox->addLayout(hbox_1);
main_vbox->addLayout(hbox_2);
main_vbox->addLayout(hbox_3);
this->setLayout(main_vbox);
}
ModeLine::~ModeLine()
{
}
//-------------------------------------------
ModeData::ModeData(const QString &str ):name_(str)
{
}
ModeData::~ModeData()
{
}