qt画一个小窗口

看图:

上代码:

zone_window_view_ctrl.h

#include "zone_dialog.h"
#include "zone_window.h"
#include <QObject>

class ZoneWindowViewCtrl : public QObject
{
    Q_OBJECT
public:
    ZoneWindowViewCtrl();
    static ZoneWindowViewCtrl * Instance();
    void init(QWidget *parent);

private:
    void InitContion();
    void InitManagerDialog();
private:
    ZoneWindow *zoneWindow_ {nullptr};
};

 zone_window_view_ctrl.cpp

#include "zone_window_view_ctrl.h"
#include "widget.h"
#include "zone_dialog_view_ctrl.h"

ZoneWindowViewCtrl::ZoneWindowViewCtrl()
{

}

 ZoneWindowViewCtrl *ZoneWindowViewCtrl::Instance()
{
    static ZoneWindowViewCtrl *ins = new ZoneWindowViewCtrl();
    return ins;
}

void ZoneWindowViewCtrl::init(QWidget *parent)
{
    zoneWindow_ = new ZoneWindow(parent);
    zoneWindow_->init();
}

void ZoneWindowViewCtrl::InitContion()
{
    connect(zoneWindow_,&ZoneWindow::ManagerButtonClickSg,ZoneDialogViewCtrl::Instance(),&ZoneDialogViewCtrl::Init);
}

void ZoneWindowViewCtrl::InitManagerDialog()
{
    // ZoneDialogViewCtrl::Instance()->Init();
}

zone_window.h

#ifndef ZONE_WINDOW_H
#define ZONE_WINDOW_H

#include <QWidget>
#include <QGroupBox>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>

class ZoneWindow: public QWidget
{
     Q_OBJECT
public:
    ZoneWindow(QWidget *parent = nullptr);
    void init();
    void SetCombox(const QStringList &list);
    void SetLineCombox(const QStringList &list);
    void SetNumberCombox(const QStringList &list);
    void SetZoneName(const QString &name);

signals:
    void InputDataSg(const QString &data);
    void ComboBoxChangeSg(const QString &data);
    void ManagerButtonClickSg();
    void LineChangeSg(const QString &data);
    void NumberChangeSg(const QString &data);


private:
    void InitWidget();
    void InitLayout();
    void InitConnect();
    void InitStatus();

    void InputData();

private:
    QLineEdit *name_{nullptr};
    QComboBox *combox_ {nullptr};
    QComboBox *lineCombox_ {nullptr};
    QComboBox *numberCombox_ {nullptr};
    QPushButton * managerButton_{nullptr};

};

#endif // ZONE_WINDOW_H

 zone_window.cpp

#include "zone_window.h"

#include <QVBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QComboBox>
#include "widget.h"

ZoneWindow::ZoneWindow(QWidget *parent) :QWidget(parent)
{

}

void ZoneWindow::init()
{
    InitWidget();
    InitLayout();
    InitConnect();
}

void ZoneWindow::SetCombox(const QStringList &list)
{
    if (combox_ == nullptr) {
        return;
    }
    combox_->clear();
    combox_->addItems(list);
}

void ZoneWindow::SetLineCombox(const QStringList &list)
{
    if (lineCombox_ == nullptr) {
        return;
    }
    lineCombox_->clear();
    lineCombox_->addItems(list);
}

void ZoneWindow::SetNumberCombox(const QStringList &list)
{
    if (numberCombox_ == nullptr) {
        return;
    }
    numberCombox_->clear();
    numberCombox_->addItems(list);
}

void ZoneWindow::SetZoneName(const QString &name)
{

}

void ZoneWindow::InitWidget()
{
    name_ = new QLineEdit;
    combox_ = new QComboBox;
    numberCombox_ = new QComboBox;
    lineCombox_ =new QComboBox;
    managerButton_ = new QPushButton("Zone Manager...");

}

void ZoneWindow::InitLayout()
{
    QVBoxLayout *mainVbox = new QVBoxLayout;
    QVBoxLayout *vbox = new QVBoxLayout();
        QHBoxLayout *layout = new QHBoxLayout;
    QGroupBox *groupBox = new QGroupBox("zone");
    groupBox->setLayout(layout);
    vbox->addWidget(groupBox);
    vbox->addWidget(managerButton_);


    QVBoxLayout *laoutLabel = new QVBoxLayout;
    QLabel *nameLabel = new QLabel("Name");
    QLabel *stackupLabel = new QLabel("Stackup");
    laoutLabel->addWidget(nameLabel);
    laoutLabel->addWidget(stackupLabel);

    QVBoxLayout *laoutIn = new QVBoxLayout;
    laoutIn->addWidget(name_);
    laoutIn->addWidget(combox_);

    layout->addLayout(laoutLabel);
    layout->addLayout(laoutIn);

    // []
    QHBoxLayout *creationLayout = new QHBoxLayout;
    QGroupBox *creationGroupBox = new QGroupBox("zone Creation Controls");
    creationGroupBox->setLayout(creationLayout);
    creationLayout->addWidget(new QLabel("Line Lock:"));
    creationLayout->addWidget(lineCombox_);
    creationLayout->addWidget(numberCombox_);

    mainVbox->addLayout(vbox);
    mainVbox->addWidget(creationGroupBox);
    this->setLayout(mainVbox);
}

void ZoneWindow::InitConnect()
{
    connect(name_,&QLineEdit::returnPressed,this,&ZoneWindow::InputData);
    connect(combox_,&QComboBox::currentTextChanged,this,&ZoneWindow::ComboBoxChangeSg);
    connect(managerButton_,&QPushButton::clicked,this,&ZoneWindow::ManagerButtonClickSg);
    connect(lineCombox_,&QComboBox::currentTextChanged,this,&ZoneWindow::LineChangeSg);
    connect(numberCombox_,&QComboBox::currentTextChanged,this,&ZoneWindow::NumberChangeSg);
}

void ZoneWindow::InitStatus()
{

}

void ZoneWindow::InputData()
{
    emit InputDataSg(name_->text());
}

zone_dialog_view_ctrl.h

#ifndef ZONE_DIALOG_VIEW_CTRL_H
#define ZONE_DIALOG_VIEW_CTRL_H
#include <QObject>
#include "zone_dialog.h"

class ZoneDialogViewCtrl: public QObject
{
    Q_OBJECT
public:
    ZoneDialogViewCtrl();
    static ZoneDialogViewCtrl *Instance();
    void Init();
private:
    void InitConnect();
private:
    ZoneDialog *dialog_;
};

#endif // ZONE_DIALOG_VIEW_CTRL_H

zone_dialog_view_ctrl.cpp

#include "zone_dialog_view_ctrl.h"

ZoneDialogViewCtrl::ZoneDialogViewCtrl() {}

ZoneDialogViewCtrl *ZoneDialogViewCtrl::Instance()
{
    static ZoneDialogViewCtrl *ins = new ZoneDialogViewCtrl;
    return ins;
}

void ZoneDialogViewCtrl::Init()
{
    dialog_ = new ZoneDialog;
    dialog_->init();
    InitConnect();
}

void ZoneDialogViewCtrl::InitConnect()
{

}

 zone_dialog.h

#ifndef ZONE_WINDOW_H
#define ZONE_WINDOW_H

#include <QWidget>
#include <QGroupBox>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>

class ZoneWindow: public QWidget
{
     Q_OBJECT
public:
    ZoneWindow(QWidget *parent = nullptr);
    void init();
    void SetCombox(const QStringList &list);
    void SetLineCombox(const QStringList &list);
    void SetNumberCombox(const QStringList &list);
    void SetZoneName(const QString &name);

signals:
    void InputDataSg(const QString &data);
    void ComboBoxChangeSg(const QString &data);
    void ManagerButtonClickSg();
    void LineChangeSg(const QString &data);
    void NumberChangeSg(const QString &data);


private:
    void InitWidget();
    void InitLayout();
    void InitConnect();
    void InitStatus();

    void InputData();

private:
    QLineEdit *name_{nullptr};
        // QLabel *nameLabel_ {nullptr};
    QComboBox *combox_ {nullptr};
    QComboBox *lineCombox_ {nullptr};
    QComboBox *numberCombox_ {nullptr};
    QPushButton * managerButton_{nullptr};

};

#endif // ZONE_WINDOW_H

  zone_dialog.cpp

#include "zone_window.h"

#include <QVBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QComboBox>
#include "widget.h"
#include <QDebug>

ZoneWindow::ZoneWindow(QWidget *parent) :QWidget(parent)
{

}

void ZoneWindow::init()
{
    InitWidget();
    InitLayout();
    InitConnect();
}

void ZoneWindow::SetCombox(const QStringList &list)
{
    if (combox_ == nullptr) {
        return;
    }
    combox_->clear();
    combox_->addItems(list);
}

void ZoneWindow::SetLineCombox(const QStringList &list)
{
    if (lineCombox_ == nullptr) {
        return;
    }
    lineCombox_->clear();
    lineCombox_->addItems(list);
}

void ZoneWindow::SetNumberCombox(const QStringList &list)
{
    if (numberCombox_ == nullptr) {
        return;
    }
    numberCombox_->clear();
    numberCombox_->addItems(list);
}

void ZoneWindow::SetZoneName(const QString &name)
{

}

void ZoneWindow::InitWidget()
{
    name_ = new QLineEdit(this);
    // nameLabel_ = new QLabel(this);
    combox_ = new QComboBox;
    numberCombox_ = new QComboBox;
    lineCombox_ =new QComboBox;
    managerButton_ = new QPushButton("Zone Manager...");

}

void ZoneWindow::InitLayout()
{
    QVBoxLayout *mainVbox = new QVBoxLayout;
    QVBoxLayout *vbox = new QVBoxLayout();
        QHBoxLayout *layout = new QHBoxLayout;
    QGroupBox *groupBox = new QGroupBox("zone");
    groupBox->setLayout(layout);
    vbox->addWidget(groupBox);
    vbox->addWidget(managerButton_);


    QVBoxLayout *laoutLabel = new QVBoxLayout;
    QLabel *nameLabel = new QLabel("Name");
    QLabel *stackupLabel = new QLabel("Stackup");
    laoutLabel->addWidget(nameLabel);
    laoutLabel->addWidget(stackupLabel);

    QVBoxLayout *laoutIn = new QVBoxLayout;
    laoutIn->addWidget(name_);
    laoutIn->addWidget(combox_);

    layout->addLayout(laoutLabel);
    layout->addLayout(laoutIn);

    // []
    QHBoxLayout *creationLayout = new QHBoxLayout;
    QGroupBox *creationGroupBox = new QGroupBox("zone Creation Controls");
    creationGroupBox->setLayout(creationLayout);
    creationLayout->addWidget(new QLabel("Line Lock:"));
    creationLayout->addWidget(lineCombox_);
    creationLayout->addWidget(numberCombox_);

    mainVbox->addLayout(vbox);
    mainVbox->addWidget(creationGroupBox);
    this->setLayout(mainVbox);
}

void ZoneWindow::InitConnect()
{
    connect(name_,&QLineEdit::returnPressed,this,&ZoneWindow::InputData);
    connect(combox_,&QComboBox::currentTextChanged,this,&ZoneWindow::ComboBoxChangeSg);
    connect(managerButton_,&QPushButton::pressed,this,&ZoneWindow::ManagerButtonClickSg);
    connect(lineCombox_,&QComboBox::currentTextChanged,this,&ZoneWindow::LineChangeSg);
    connect(numberCombox_,&QComboBox::currentTextChanged,this,&ZoneWindow::NumberChangeSg);

    // 切换按钮1和按钮2
    // QObject::connect(&button1, &QPushButton::clicked, [&]() {
    //     layout->removeWidget(&button1);
    //     layout->addWidget(&button2);
    // });

    // QObject::connect(&button2, &QPushButton::clicked, [&]() {
    //     layout->removeWidget(&button2);
    //     layout->addWidget(&button1);
    // });
}

void ZoneWindow::InitStatus()
{

}

void ZoneWindow::InputData()
{
    emit InputDataSg(name_->text());
}

代理:

#ifndef ZONE_DELEAGTE
#define ZONE_DELEAGTE

#include <QItemDelegate>
#include <QComboBox>
#include <QDebug>
#include <QLabel>
#include <QToolButton>
#include <QRadioButton>
#include <QLabel>

class ComboBoxDelegate : public QItemDelegate {
public:
    ComboBoxDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}

    // 创建编辑器
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        if (!index.isValid())
            return nullptr;

        // 创建一个下拉框
        if (index.column() == 0) {
            QRadioButton * button = new QRadioButton(parent);
            return button;
        } else if (index.column() == 1) {
            QLabel * label = new QLabel(parent);
            return label;
        } else if(index.column() == 2){
            QComboBox *comboBox = new QComboBox(parent);
            comboBox->addItem("Option 1");
            comboBox->addItem("Option 2");
            comboBox->addItem("Option 3");
            return comboBox;
        }

    }

    // 设置编辑器数据
    void setEditorData(QWidget *editor, const QModelIndex &index) const override {
        if (!index.isValid())
            return;

        // 从模型中获取数据并设置给下拉框
        if (index.column() == 2) {
            QString value = index.model()->data(index, Qt::EditRole).toString();
            QComboBox *comboBox = static_cast<QComboBox *>(editor);
            comboBox->setCurrentText(value);
        }
    }

    // 将编辑器的数据写回到模型
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
        if (!index.isValid()) {
            return;
        }
        if (index.column() == 0 || index.column() == 1) {
            QComboBox *comboBox = static_cast<QComboBox *>(editor);
            QString value = comboBox->currentText();
            model->setData(index, value, Qt::EditRole);
        }
    }

    // 更新编辑器的几何属性
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        if (!index.isValid())
            return;
        editor->setGeometry(option.rect);
    }
};
#endif

关于同一个位置切换控件:

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建窗口和布局
    QWidget window;
    QVBoxLayout *layout = new QVBoxLayout(&window);

    // 创建两个控件
    QPushButton button1("按钮1");
    QPushButton button2("按钮2");

    // 将按钮1添加到布局中
    layout->addWidget(&button1);

    // 显示窗口
    window.show();

    // 切换按钮1和按钮2
    QObject::connect(&button1, &QPushButton::clicked, [&]() {
        layout->removeWidget(&button1);
        layout->addWidget(&button2);
    });

    QObject::connect(&button2, &QPushButton::clicked, [&]() {
        layout->removeWidget(&button2);
        layout->addWidget(&button1);
    });

    return app.exec();
}



、、 ---------------------------------------------------

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>

class ClickableLabel : public QLabel {
    Q_OBJECT
public:
    explicit ClickableLabel(QWidget *parent = nullptr) : QLabel(parent) {}

signals:
    void doubleClicked();

protected:
    void mouseDoubleClickEvent(QMouseEvent *event) override {
        emit doubleClicked();
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QWidget window;
    QVBoxLayout *layout = new QVBoxLayout(&window);

    ClickableLabel label("双击我");
    label.setAlignment(Qt::AlignCenter);

    layout->addWidget(&label);
    window.show();

    QObject::connect(&label, &ClickableLabel::doubleClicked, [&]() {
        qDebug() << "Label 被双击了!";
    });

    return app.exec();
}

#include "main.moc"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值