240_QT_创建一个非模态的窗体框架,可以定位table view的方框位置,以及填入内容

65 篇文章 6 订阅

模态:生命周期和我们的主窗体是一致的,且一次创建后,就无法再实时操作了
非模态:生命周期是自己决定的,可以随时操作主窗体上设置好的table view中的内容

图示

在这里插入图片描述
在这里插入图片描述

再点击❌号、关闭按键的时候,主窗体的该按键,依然可选

在这里插入图片描述
.
.
.在这里插入图片描述

代码

“qdialoglocate.h”

#ifndef QDIALOGLOCATE_H
#define QDIALOGLOCATE_H

#include <QDialog>
#include <QMessageBox>
#include <mainwindow.h>
namespace Ui {
class QDialogLocate;
}

class QDialogLocate : public QDialog
{
    Q_OBJECT

public:
    explicit QDialogLocate(QWidget *parent = nullptr);
    ~QDialogLocate();

    void setRange(int row, int col);
    void setValue(int row, int col);
    void closeEvent(QCloseEvent *event);/*注意,这是个重载函数*/
    void refrashMainWindow();
private slots:
     void on_btnSetText_clicked();
     void on_btnClose_clicked();
    // void receiveBool(bool ok);
signals:
     void changeCellText(int , int , QString);
     //void changeEnable(bool ok);
private:
    Ui::QDialogLocate *ui;
};

#endif // QDIALOGLOCATE_H

qdialoglocate.cpp

#include "qdialoglocate.h"
#include "ui_qdialoglocate.h"

QDialogLocate::QDialogLocate(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QDialogLocate)
{
    ui->setupUi(this);
    connect(ui->btnClose, &QPushButton::clicked, this,  &QDialogLocate::reject );
}

QDialogLocate::~QDialogLocate()
{
    QString digtitle = QString::fromLocal8Bit("提示");
    QString textTitle = QString::fromLocal8Bit("设置对话框退出");
    QMessageBox::information(this,digtitle, textTitle );
    delete ui;
}

void QDialogLocate::setRange(int row, int col)
{
    ui->spinBoxRow->setRange(0, row -1);
    ui->spinBoxColumn->setRange(0, col -1);
}

void QDialogLocate::setValue(int row, int col)
{
    ui->spinBoxRow->setValue(row);
    ui->spinBoxColumn->setValue(col);

}

//这里是点击右上角×号之后,使得主界面图标依然显示,且释放当前窗体框架
void QDialogLocate::closeEvent(QCloseEvent *event)
{
    /*当每次重新打开主窗体的时候,将控件重新显示为可操作且重新构建*/
    MainWindow *    parW =static_cast<MainWindow *>(parentWidget());
    parW->setActLocateEnable(true);
    parW->setDlgLocateNull();

}

//这里是点击窗体的退出按键之后,使得主界面图标依然显示,且释放当前窗体框架
void QDialogLocate::refrashMainWindow()
{
    /*当每次重新打开主窗体的时候,将控件重新显示为可操作且重新构建*/
    MainWindow *    parW =static_cast<MainWindow *>(parentWidget());
    parW->setActLocateEnable(true);
    parW->setDlgLocateNull();
}

void QDialogLocate::on_btnSetText_clicked()
{
    int row = ui->spinBoxRow->value();
    int col = ui->spinBoxColumn->value();

     //MainWindow * parW =static_cast<MainWindow *>(parentWidget());
     //parW->setACellText(row, col, ui->edtCaption->text());

     emit changeCellText(row, col, ui->edtCaption->text());

     if(ui->chkBoxRow->isChecked())
        ui->spinBoxRow->setValue(ui->spinBoxRow->value() + 1);
     if(ui->chkBoxColumn->isChecked())
         ui->spinBoxColumn->setValue(ui->spinBoxColumn->value() + 1);
}


void QDialogLocate::on_btnClose_clicked()
{
    refrashMainWindow();
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qdialoglocate.h"
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "dialogsetheaders.h"
#include <QItemSelectionModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class QDialogLocate;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    MainWindow *self(){return m_self;}
    void setActLocateEnable(bool ok);
    void setDlgLocateNull();
    //void setACellText(int row, int col, QString text);

private slots:
    void on_actTab_SetSize_triggered();

    void on_actTab_SetHeader_triggered();

    void on_actTab_Locate_triggered();

    void on_tableView_clicked(const QModelIndex &index);
    void setACellText(int row, int col, QString text);
private:
    Ui::MainWindow *ui;
    static MainWindow * m_self;
    QStandardItemModel *theModel;
    QItemSelectionModel *theSelection;
    DialogSetHeaders * dialogHeaders = nullptr;
    QDialogLocate * dialogLocate = NULL;
};
#endif // MAINWINDOW_H

mainwindow.cpp—on_actTab_Locate_triggered()

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
/*
 * 多个对话框组成
 *      * 每个对话框都有属于自己的ui
*/
MainWindow* MainWindow::m_self = nullptr;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_self = this;
    theModel = new QStandardItemModel(this);
    theSelection = new QItemSelectionModel(theModel);
    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelection);

    if(!ui->actTab_Locate->isEnabled())
            ui->actTab_Locate->setEnabled(true);
}

MainWindow::~MainWindow()
{
    delete ui;
    m_self = nullptr;
}

//设置行数列表---人为删除
void MainWindow::on_actTab_SetSize_triggered()
{
    /*
     * 创建一个模态式的对话框:
     * 退出对话框之后便无法再继续操作
    */
    Dialog *dig = new Dialog(this);
    /*启动,将table view中的初始行、列显示到对话框*/
    dig->SetRowColumns(theModel->rowCount(), theModel->columnCount());

    int ret = dig->exec();
    if(ret == QDialog::Accepted)
    {
        int col = dig->columCount();
        int row = dig->rowCount();
        theModel->setColumnCount(col);/*通过themodel,设置table view的行列*/
        theModel->setRowCount(row);
    }

    delete  dig;
}

//设置标题--自己删除自己,随着整体view退出而退出
void MainWindow::on_actTab_SetHeader_triggered()
{
    if(dialogHeaders == nullptr)
        dialogHeaders = new DialogSetHeaders(this);

    /*先拿到当前表格数据*/
    if(dialogHeaders->strigList().count() != theModel->columnCount())
    {
        QStringList strlist;
        for(int i = 0; i < theModel->rowCount(); i++){
            strlist << theModel->headerData(i, Qt::Horizontal).toString();
        }
        dialogHeaders->setStringList(strlist);

    }

    /*阻塞住,把标题框中数据设置到表格*/
    int ret = dialogHeaders->exec();
    if(ret == QDialog::Accepted){
        QStringList strlist = dialogHeaders->strigList();
        theModel->setHorizontalHeaderLabels(strlist);
    }
}


void MainWindow::on_actTab_Locate_triggered()
{
    ui->actTab_Locate->setEnabled(false);

    if(dialogLocate == nullptr)
        dialogLocate = new QDialogLocate(this);

    dialogLocate->setAttribute(Qt::WA_DeleteOnClose);
    Qt::WindowFlags flags = dialogLocate->windowFlags();/*获取一下当前窗体的状态*/
    dialogLocate->setWindowFlags(flags | Qt::WindowStaysOnTopHint);/*当前窗口设置一个新的状态,始终在最上层*/
    dialogLocate->setRange(theModel->rowCount(), theModel->columnCount());

    QModelIndex curIndex = theSelection->currentIndex();/*获取当前界面索引*/
    if(curIndex.isValid())
        dialogLocate->setValue(curIndex.row(), curIndex.column());

    connect(dialogLocate, &QDialogLocate::changeCellText , this, &MainWindow::setACellText);

    dialogLocate->show();


}

void MainWindow::setActLocateEnable(bool ok)
{
    ui->actTab_Locate->setEnabled(ok);
}

void MainWindow::setDlgLocateNull()
{
   if(dialogLocate != nullptr)
        dialogLocate= nullptr;
}

void MainWindow::setACellText(int row, int col, QString text)
{
    QModelIndex index = theModel->index(row, col);
    theModel->setData(index, text , Qt::DisplayRole);
    /*重新设置一下选择的状态*/
    theSelection->clearSelection();
    theSelection->setCurrentIndex(index, QItemSelectionModel::Select);
}


void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    if(dialogLocate != NULL)
        dialogLocate->setValue(index.row(), index.column());
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值