17、自定义对话框及其调用

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

1、samp6_2.pro

#-------------------------------------------------
#
# Project created by QtCreator 2016-12-10T17:40:29
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = samp6_2
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    qwdialogsize.cpp \
    qwdialogheaders.cpp \
    qwdialoglocate.cpp

HEADERS  += mainwindow.h \
    qwdialogsize.h \
    qwdialogheaders.h \
    qwdialoglocate.h

FORMS    += mainwindow.ui \
    qwdialogsize.ui \
    qwdialogheaders.ui \
    qwdialoglocate.ui

RESOURCES += \
    res.qrc

2、main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>567</width>
    <height>367</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>自定义对话框及其调用</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QTableView" name="tableView">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>10</y>
      <width>421</width>
      <height>211</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>567</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <property name="toolButtonStyle">
    <enum>Qt::ToolButtonTextUnderIcon</enum>
   </property>
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
   <addaction name="actTab_SetSize"/>
   <addaction name="actTab_SetHeader"/>
   <addaction name="actTab_Locate"/>
   <addaction name="actFile_Quit"/>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="actTab_SetSize">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/230.bmp</normaloff>:/images/images/230.bmp</iconset>
   </property>
   <property name="text">
    <string>设置行数列数</string>
   </property>
   <property name="toolTip">
    <string>设置表格行数列数</string>
   </property>
  </action>
  <action name="actTab_SetHeader">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/516.bmp</normaloff>:/images/images/516.bmp</iconset>
   </property>
   <property name="text">
    <string>设置表头标题</string>
   </property>
   <property name="toolTip">
    <string>设置表头标题</string>
   </property>
  </action>
  <action name="actFile_Quit">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/132.bmp</normaloff>:/images/images/132.bmp</iconset>
   </property>
   <property name="text">
    <string>退出</string>
   </property>
   <property name="toolTip">
    <string>退出本软件</string>
   </property>
  </action>
  <action name="actTab_Locate">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/304.bmp</normaloff>:/images/images/304.bmp</iconset>
   </property>
   <property name="text">
    <string>定位单元格</string>
   </property>
   <property name="toolTip">
    <string>定位到某个单元格</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>actFile_Quit</sender>
   <signal>triggered()</signal>
   <receiver>MainWindow</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>-1</x>
     <y>-1</y>
    </hint>
    <hint type="destinationlabel">
     <x>397</x>
     <y>255</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


#include    <QLabel>
#include    <QStandardItemModel>
#include    <QItemSelectionModel>
#include    <QModelIndex>

#include    "qwdialogheaders.h"
#include    "qwdialoglocate.h"

class   QWDialogLocate;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

private:

    //用于状态栏的信息显示
    QLabel  *LabCellPos;    //当前单元格行列号
    QLabel  *LabCellText;   //当前单元格内容

    QStandardItemModel  *theModel;//数据模型
    QItemSelectionModel *theSelection;//Item选择模型

    QWDialogHeaders *dlgSetHeaders=NULL;//设置表头文字对话框, 一次创建,exec()重复调用
    QWDialogLocate  *dlgLocate=NULL;//定位单元格对话框,show()调用,关闭时自己删除

    void closeEvent(QCloseEvent *event);//关闭窗口时间,可以询问是否退出

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void    setActLocateEnable(bool enable);//设置actTab_Locate的enabled属性
    void    setACellText(int row,int column,QString text);//设置一个单元格的内容,由dlgLocate调用
    void    setDlgLocateNull();//将dlgLocate指针设置为NULL
private slots:
    void on_currentChanged(const QModelIndex &current, const QModelIndex &previous);

    void on_actTab_SetSize_triggered();

    void on_actTab_SetHeader_triggered();

    void on_actTab_Locate_triggered();

    void on_tableView_clicked(const QModelIndex &index);

private:
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include    <QMessageBox>
#include    <QCloseEvent>

#include    "qwdialogsize.h"


void MainWindow::closeEvent(QCloseEvent *event)
{ //窗口关闭时询问是否退出
   QMessageBox::StandardButton result=QMessageBox::question(this, "确认", "确定要退出本程序吗?",
                      QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                      QMessageBox::No);

    if (result==QMessageBox::Yes)
        event->accept();
    else
        event->ignore();
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    theModel = new QStandardItemModel(10,5,this); //创建数据模型
    theSelection = new QItemSelectionModel(theModel);//Item选择模型
    connect(theSelection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this,SLOT(on_currentChanged(QModelIndex,QModelIndex)));

    //为tableView设置数据模型
    ui->tableView->setModel(theModel); //设置数据模型
    ui->tableView->setSelectionModel(theSelection);//设置选择模型

    setCentralWidget(ui->tableView); //

//创建状态栏组件
    LabCellPos = new QLabel("当前单元格:",this);
    LabCellPos->setMinimumWidth(180);
    LabCellPos->setAlignment(Qt::AlignHCenter);

    LabCellText = new QLabel("单元格内容:",this);
    LabCellText->setMinimumWidth(200);


    ui->statusBar->addWidget(LabCellPos);
    ui->statusBar->addWidget(LabCellText);
}

MainWindow::~MainWindow()
{
    delete ui;
}


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

void MainWindow::setACellText(int row, int column, QString text)
{//定位到单元格,并设置字符串
    QModelIndex index=theModel->index(row,column);//获取模型索引
    theSelection->clearSelection(); //清除现有选择
    theSelection->setCurrentIndex(index,QItemSelectionModel::Select); //定位到单元格
    theModel->setData(index,text,Qt::DisplayRole);//设置单元格字符串
}

void MainWindow::setDlgLocateNull()
{
    dlgLocate=NULL;
}


void MainWindow::on_currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
    if (current.isValid()) //当前模型索引有效
    {
        LabCellPos->setText(QString::asprintf("当前单元格:%d行,%d列",
                                  current.row(),current.column())); //显示模型索引的行和列号
        QStandardItem   *aItem;
        aItem=theModel->itemFromIndex(current); //从模型索引获得Item
        this->LabCellText->setText("单元格内容:"+aItem->text()); //显示item的文字内容
    }
}

void MainWindow::on_actTab_SetSize_triggered()
{ //模态对话框,动态创建,用过后删除
    QWDialogSize    *dlgTableSize=new QWDialogSize(this); //创建对话框
//   dlgTableSize->setAttribute(Qt::WA_DeleteOnClose);
//对话框关闭时自动删除对话框对象,用于不需要读取返回值的对话框
//如果需要获取对话框的返回值,不能设置该属性,可以在调用完对话框后删除对话框
    Qt::WindowFlags    flags=dlgTableSize->windowFlags();
    dlgTableSize->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //设置对话框固定大小

    dlgTableSize->setRowColumn(theModel->rowCount(),theModel->columnCount()); //对话框数据初始化

    int ret=dlgTableSize->exec();// 以模态方式显示对话框,用户关闭对话框时返回 DialogCode值
    if (ret==QDialog::Accepted) //OK键被按下,对话框关闭,若设置了setAttribute(Qt::WA_DeleteOnClose),对话框被释放,无法获得返回值
    { //OK键被按下,获取对话框上的输入,设置行数和列数
        int cols=dlgTableSize->columnCount();
        theModel->setColumnCount(cols);

        int rows=dlgTableSize->rowCount();
        theModel->setRowCount(rows);
    }
    delete dlgTableSize; //删除对话框
}

void MainWindow::on_actTab_SetHeader_triggered()
{//一次创建,多次调用,对话框关闭时只是隐藏
    if (dlgSetHeaders==NULL) //如果对象没有被创建过,就创建对象
        dlgSetHeaders = new QWDialogHeaders(this);

    if (dlgSetHeaders->headerList().count()!=theModel->columnCount())
    {//如果表头列数变化,重新初始化
        QStringList strList;
        for (int i=0;i<theModel->columnCount();i++)//获取现有的表头标题
            strList.append(theModel->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString());
        dlgSetHeaders->setHeaderList(strList);//用于对话框初始化显示
    }

    int ret=dlgSetHeaders->exec();// 以模态方式显示对话框
    if (ret==QDialog::Accepted) //OK键被按下
    {
        QStringList strList=dlgSetHeaders->headerList();//获取对话框上修改后的StringList
        theModel->setHorizontalHeaderLabels(strList);// 设置模型的表头标题
    }
}

void MainWindow::on_actTab_Locate_triggered()
{//创建 StayOnTop的对话框,对话框关闭时自动删除
 //通过控制actTab_Locate的enable属性避免重复点击
    ui->actTab_Locate->setEnabled(false);

    dlgLocate = new QWDialogLocate(this); //创建对话框,传递指针
    dlgLocate->setAttribute(Qt::WA_DeleteOnClose); //对话框关闭时自动删除对话框对象,用于不需要读取返回值的对话框
    Qt::WindowFlags    flags=dlgLocate->windowFlags(); //获取已有flags
//对话框设置为固定大小和StayOnTop
//    dlgLocate->setWindowFlags(flags |Qt::MSWindowsFixedSizeDialogHint |Qt::WindowStaysOnTopHint); //设置对话框固定大小,StayOnTop
    dlgLocate->setWindowFlags(flags | Qt::WindowStaysOnTopHint); //设置对话框固定大小,StayOnTop
//对话框初始化设置
    dlgLocate->setSpinRange(theModel->rowCount(),theModel->columnCount());
    QModelIndex curIndex=theSelection->currentIndex();
    if (curIndex.isValid())
       dlgLocate->setSpinValue(curIndex.row(),curIndex.column());

    dlgLocate->show(); //非模态显示对话框
}

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{//单击单元格时,将单元格的行号、列号设置到对话框上
    if (dlgLocate!=NULL) //对话框存在
        dlgLocate->setSpinValue(index.row(),index.column());
}

qwdialogheaders.h

#ifndef QWDIALOGHEADERS_H
#define QWDIALOGHEADERS_H

#include <QDialog>

#include    <QStringListModel>

namespace Ui {
class QWDialogHeaders;
}

class QWDialogHeaders : public QDialog
{
    Q_OBJECT

private:
    QStringListModel  *model;

public:
    explicit QWDialogHeaders(QWidget *parent = 0);
    ~QWDialogHeaders();

    void    setHeaderList(QStringList& headers);
    QStringList headerList();
private:
    Ui::QWDialogHeaders *ui;
};

#endif // QWDIALOGHEADERS_H

qwdialogheaders.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QWDialogHeaders</class>
 <widget class="QDialog" name="QWDialogHeaders">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>287</width>
    <height>303</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <pointsize>10</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>设置表头标题</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_3">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QGroupBox" name="groupBox">
       <property name="title">
        <string>表头标题</string>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout">
        <property name="spacing">
         <number>4</number>
        </property>
        <property name="leftMargin">
         <number>5</number>
        </property>
        <property name="topMargin">
         <number>5</number>
        </property>
        <property name="rightMargin">
         <number>5</number>
        </property>
        <property name="bottomMargin">
         <number>5</number>
        </property>
        <item>
         <widget class="QListView" name="listView"/>
        </item>
       </layout>
      </widget>
     </item>
     <item>
      <widget class="QFrame" name="frame">
       <property name="frameShape">
        <enum>QFrame::StyledPanel</enum>
       </property>
       <property name="frameShadow">
        <enum>QFrame::Raised</enum>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <spacer name="verticalSpacer_4">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <widget class="QPushButton" name="btnOK">
          <property name="text">
           <string>确定</string>
          </property>
          <property name="icon">
           <iconset resource="res.qrc">
            <normaloff>:/images/images/704.bmp</normaloff>:/images/images/704.bmp</iconset>
          </property>
         </widget>
        </item>
        <item>
         <spacer name="verticalSpacer_2">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <widget class="QPushButton" name="btnCancel">
          <property name="text">
           <string>取消</string>
          </property>
          <property name="icon">
           <iconset resource="res.qrc">
            <normaloff>:/images/images/706.bmp</normaloff>:/images/images/706.bmp</iconset>
          </property>
         </widget>
        </item>
        <item>
         <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>28</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <spacer name="verticalSpacer_6">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <spacer name="verticalSpacer_5">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <spacer name="verticalSpacer_3">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>20</width>
            <height>40</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>btnOK</sender>
   <signal>clicked()</signal>
   <receiver>QWDialogHeaders</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>277</x>
     <y>73</y>
    </hint>
    <hint type="destinationlabel">
     <x>167</x>
     <y>168</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>btnCancel</sender>
   <signal>clicked()</signal>
   <receiver>QWDialogHeaders</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>277</x>
     <y>146</y>
    </hint>
    <hint type="destinationlabel">
     <x>167</x>
     <y>168</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

qwdialogheaders.cpp

#include "qwdialogheaders.h"
#include "ui_qwdialogheaders.h"

#include    <QMessageBox>

QWDialogHeaders::QWDialogHeaders(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QWDialogHeaders)
{
    ui->setupUi(this);
    model= new QStringListModel;
    ui->listView->setModel(model);
}

QWDialogHeaders::~QWDialogHeaders()
{
    QMessageBox::information(this,"提示","设置表头标题对话框被删除");
    delete ui;
}

void QWDialogHeaders::setHeaderList(QStringList &headers)
{
    model->setStringList(headers);
}

QStringList QWDialogHeaders::headerList()
{
    return  model->stringList();
}

qwdialoglocate.h

#ifndef QWDIALOGLOCATE_H
#define QWDIALOGLOCATE_H

#include <QDialog>

namespace Ui {
class QWDialogLocate;
}

class QWDialogLocate : public QDialog
{
    Q_OBJECT

private:
    void closeEvent(QCloseEvent *event);
    void showEvent(QShowEvent *event);

public:
    explicit QWDialogLocate(QWidget *parent = 0);
    ~QWDialogLocate();

    void    setSpinRange(int rowCount, int colCount); //设置最大值
    void    setSpinValue(int rowNo, int colNo);//设置初始值

private slots:
    void on_btnSetText_clicked();

private:
    Ui::QWDialogLocate *ui;
};

#endif // QWDIALOGLOCATE_H

qwdialoglocate.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QWDialogLocate</class>
 <widget class="QDialog" name="QWDialogLocate">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>332</width>
    <height>142</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <pointsize>10</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>单元格定位与文字设置</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="QGroupBox" name="groupBox">
     <property name="title">
      <string/>
     </property>
     <layout class="QGridLayout" name="gridLayout">
      <item row="0" column="0">
       <widget class="QLabel" name="label">
        <property name="text">
         <string>列  号</string>
        </property>
       </widget>
      </item>
      <item row="0" column="1">
       <widget class="QSpinBox" name="spinBoxColumn">
        <property name="minimum">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="0" column="2">
       <widget class="QCheckBox" name="chkBoxColumn">
        <property name="text">
         <string>列增</string>
        </property>
       </widget>
      </item>
      <item row="1" column="0">
       <widget class="QLabel" name="label_2">
        <property name="text">
         <string>行  号</string>
        </property>
       </widget>
      </item>
      <item row="1" column="1">
       <widget class="QSpinBox" name="spinBoxRow"/>
      </item>
      <item row="1" column="2">
       <widget class="QCheckBox" name="chkBoxRow">
        <property name="text">
         <string>行增</string>
        </property>
       </widget>
      </item>
      <item row="2" column="0">
       <widget class="QLabel" name="label_3">
        <property name="text">
         <string>设定文字</string>
        </property>
       </widget>
      </item>
      <item row="2" column="1" colspan="2">
       <widget class="QLineEdit" name="edtCaption"/>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QPushButton" name="btnSetText">
       <property name="text">
        <string>设定文字</string>
       </property>
       <property name="icon">
        <iconset resource="res.qrc">
         <normaloff>:/images/images/506.bmp</normaloff>:/images/images/506.bmp</iconset>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="btnClose">
       <property name="text">
        <string>关  闭</string>
       </property>
       <property name="icon">
        <iconset resource="res.qrc">
         <normaloff>:/images/images/132.bmp</normaloff>:/images/images/132.bmp</iconset>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>btnClose</sender>
   <signal>clicked()</signal>
   <receiver>QWDialogLocate</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>274</x>
     <y>108</y>
    </hint>
    <hint type="destinationlabel">
     <x>243</x>
     <y>134</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

qwdialoglocate.cpp

#include "qwdialoglocate.h"
#include "ui_qwdialoglocate.h"

#include    <QCloseEvent>
#include    "mainwindow.h"
#include    <QMessageBox>

void QWDialogLocate::closeEvent(QCloseEvent *event)
{ //窗口关闭事件,关闭时释放本窗口
    MainWindow *parWind = (MainWindow*)parentWidget(); //获取父窗口指针
    parWind->setActLocateEnable(true);//使能 actTab_Locate
    parWind->setDlgLocateNull(); //将窗口指针设置为NULL
}

void QWDialogLocate::showEvent(QShowEvent *event)
{//窗口显示事件
    MainWindow *parWind = (MainWindow*)parentWidget();//获取
    parWind->setActLocateEnable(false);
}

QWDialogLocate::QWDialogLocate(QWidget *parent) :
    QDialog(parent), ui(new Ui::QWDialogLocate)
{//构造函数
    ui->setupUi(this);
}

QWDialogLocate::~QWDialogLocate()
{//析构函数
    QMessageBox::information(this,"提示","单元格定位对话框被删除");
    delete ui;
}


void QWDialogLocate::setSpinRange(int rowCount, int colCount)
{//设置SpinBox输入最大值
    ui->spinBoxRow->setMaximum(rowCount-1);
    ui->spinBoxColumn->setMaximum(colCount-1);
}

void QWDialogLocate::setSpinValue(int rowNo, int colNo)
{//设置SpinBox数值
    ui->spinBoxRow->setValue(rowNo);
    ui->spinBoxColumn->setValue(colNo);
}

void QWDialogLocate::on_btnSetText_clicked()
{//定位到单元格,并设置字符串
    int row=ui->spinBoxRow->value(); //行号
    int col=ui->spinBoxColumn->value();//列号

    MainWindow *parWind = (MainWindow*)parentWidget(); //获取主窗口
    parWind->setACellText(row,col,ui->edtCaption->text()); //设置单元格文字
    if (ui->chkBoxRow->isChecked()) //行增
        ui->spinBoxRow->setValue(1+ui->spinBoxRow->value());

    if (ui->chkBoxColumn->isChecked()) //列增
        ui->spinBoxColumn->setValue(1+ui->spinBoxColumn->value());
}

qwdialogsize.h

#ifndef QWDIALOGSIZE_H
#define QWDIALOGSIZE_H

#include <QDialog>

namespace Ui {
class QWDialogSize;
}

class QWDialogSize : public QDialog
{
    Q_OBJECT

public:
    explicit QWDialogSize(QWidget *parent = 0);
    ~QWDialogSize();

    int     rowCount();//获取对话框输入的行数
    int     columnCount();//获取对话框输入的列数
    void    setRowColumn(int row, int column); //初始对话框上两个SpinBox的值

private slots:

private:
    Ui::QWDialogSize *ui;
};

#endif // QWDIALOGSIZE_H

qwdialogsize.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QWDialogSize</class>
 <widget class="QDialog" name="QWDialogSize">
  <property name="windowModality">
   <enum>Qt::NonModal</enum>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>273</width>
    <height>120</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="font">
   <font>
    <pointsize>10</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>设置表格行数和列数</string>
  </property>
  <property name="sizeGripEnabled">
   <bool>false</bool>
  </property>
  <property name="modal">
   <bool>false</bool>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>171</width>
     <height>101</height>
    </rect>
   </property>
   <property name="title">
    <string>设置表格行数和列数</string>
   </property>
   <layout class="QFormLayout" name="formLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label_2">
      <property name="text">
       <string>列  数</string>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QSpinBox" name="spinBoxColumn">
      <property name="minimum">
       <number>1</number>
      </property>
      <property name="maximum">
       <number>500</number>
      </property>
      <property name="value">
       <number>6</number>
      </property>
     </widget>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="label">
      <property name="text">
       <string>行  数</string>
      </property>
     </widget>
    </item>
    <item row="2" column="1">
     <widget class="QSpinBox" name="spinBoxRow">
      <property name="minimum">
       <number>1</number>
      </property>
      <property name="maximum">
       <number>500</number>
      </property>
      <property name="value">
       <number>10</number>
      </property>
     </widget>
    </item>
    <item row="1" column="1">
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>40</height>
       </size>
      </property>
     </spacer>
    </item>
   </layout>
  </widget>
  <widget class="QPushButton" name="btnCancel">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>80</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>取消</string>
   </property>
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/706.bmp</normaloff>:/images/images/706.bmp</iconset>
   </property>
  </widget>
  <widget class="QPushButton" name="btnOK">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>20</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>确定</string>
   </property>
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/images/704.bmp</normaloff>:/images/images/704.bmp</iconset>
   </property>
  </widget>
 </widget>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>btnOK</sender>
   <signal>clicked()</signal>
   <receiver>QWDialogSize</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>231</x>
     <y>33</y>
    </hint>
    <hint type="destinationlabel">
     <x>272</x>
     <y>31</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>btnCancel</sender>
   <signal>clicked()</signal>
   <receiver>QWDialogSize</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>192</x>
     <y>80</y>
    </hint>
    <hint type="destinationlabel">
     <x>197</x>
     <y>119</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

qwdialogsize.cpp

#include "qwdialogsize.h"
#include "ui_qwdialogsize.h"

#include    <QMessageBox>

QWDialogSize::QWDialogSize(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QWDialogSize)
{
    ui->setupUi(this);
}

QWDialogSize::~QWDialogSize()
{
    QMessageBox::information(this,"提示","设置表格行列数对话框被删除");
    delete ui;
}

int QWDialogSize::rowCount()
{ //用于主窗口调用获得行数的输入值
    return  ui->spinBoxRow->value();
}

int QWDialogSize::columnCount()
{//用于主窗口调用获得列数的输入值
    return  ui->spinBoxColumn->value();
}

void QWDialogSize::setRowColumn(int row, int column)
{ //初始化数据显示
    ui->spinBoxRow->setValue(row);
    ui->spinBoxColumn->setValue(column);
}


源码:https://download.csdn.net/download/qq_30457077/85075848

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值