第11篇Qt实现安装向导对话框之代码布局篇(四)第11篇 Qt实现安装向导对话框之代码布局篇(四)
1.源⽂件分段解说
1.7.第四个页⾯创建函数
void Widget::createPage_four()
{
this->page_four =new QWizardPage;
this->page_four->setSubTitle("激活");
QGridLayout* glayout =new QGridLayout;
glayout->addWidget(new QLabel("激活⽅式:"),0,0);
activation_mode =new QComboBox;
activation_mode->addItem("序列号");
activation_mode->addItem("登陆账号");
QObject::connect(activation_mode,SIGNAL(currentIndexChanged(int)),this,SLOT(changeActivation(int)));
glayout->addWidget(activation_mode,0,1);
this->enter_widget =new QListWidget(this->page_four);
QGridLayout* enterlayout =new QGridLayout;
enterlayout->addWidget(new QLabel("⽤户名"),0,0);
QLineEdit* user =new QLineEdit;
user->setMaxLength(15);
enterlayout->addWidget(user,0,1);
enterlayout->addWidget(new QLabel("密码"),1,0);
QLineEdit* password =new QLineEdit;
password->setMaxLength(15);
password->setEchoMode(QLineEdit::Password);
enterlayout->addWidget(password,1,1);
this->enter_widget->setLayout(enterlayout);
this->serialnumber_widget =new QListWidget(this->page_four);
QGridLayout* serlayout =new QGridLayout;
serlayout->addWidget(new QLabel("序列号"),0,0);
QLineEdit* number =new QLineEdit;
number->setMaxLength(15);
number->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA;#");
serlayout->addWidget(number,0,1);
this->serialnumber_widget->setLayout(serlayout);
this->activate_widget =new QStackedWidget(this->page_four);
box shadow怎么设置glayout->addWidget(this->activate_widget,1,0,1,2);
this->activate_widget->addWidget(this->serialnumber_widget);
this->activate_widget->addWidget(this->enter_widget);
glayout->addWidget(new QPushButton("激活"),2,0);
QFrame* vline =new QFrame(this->page_four);
vline->setFrameShape(QFrame::HLine);
vline->setFrameShadow(QFrame::Sunken);
glayout->addWidget(vline,3,0,1,2);
glayout->addWidget(new QCheckBox("跳过"),4,1);
this->page_four->setLayout(glayout);
}
代码长了不好说,把图放在这⾥看吧,第⼀部分就是最顶端的激活⽅式和序列号和登录账号的设置,和对QComboBox按钮的槽函数链接,以达到切换时的界⾯切换效果。关于StackedWidget的使⽤可以去搜索⼀下,有很多⽂章,这⾥就不赘述了。
第⼆部分是对登录账号⽅式界⾯的布局,第三是序列号⽅式的布局,为了⽅便我都⽤的是⽹格布局⽅式。
number->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA;#");这句话的意思是设置输⼊⽅式为序列号,还有很多种输⼊⽅式,需要了解的到帮助⽂档查⼀下就可以了。
第四部分是把布局好的两个界⾯加⼊QStackedWidget中,以便可以通过它来进⾏页⾯切换。
第五部分是加⼊⼀条线,我其实很奇怪啊,当时我不知道这条线怎么加,去UI哪⾥拖动之后看到线条对应的类名是QLine,但是⽤代码添加不对,然后去问⽼师,他让我去看.ui⽂件对应的头⽂件,最后才看到原来是通过QFram来实现的,细节之后再说。
1.8.⽂件夹及⽂件的遍历函数
void Widget::findFile(QString filepath)
{
QDir dir(filepath);
if(!ists()){
return;
}
//取到所有的⽂件和⽂件名,但是去掉.和..的⽂件夹(这是QT默认有的)
dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
//⽂件夹优先
dir.setSorting(QDir::DirsFirst);
//转化成⼀个list
QFileInfoList list = InfoList();
if(list.size()<1){
return;
}
int i=0;
//递归算法的核⼼部分
do{
this->progress_value++;
this->progress->setValue(this->progress_value);
QThread::msleep(300);
QFileInfo fileInfo = list.at(i);
this->filelist->insertPlainText(fileInfo.absoluteFilePath()+'\n');//将路径显⽰在安装⽂件列表中
//如果是⽂件夹,递归
bool bisDir = fileInfo.isDir();
if(bisDir){
findFile(fileInfo.filePath());
}
i++;
}while(i < list.size());
}
这个是复制⽐⼈的代码改动的,博客⾥有很多⽂章。可以看到是通过递归实现的,因为如果是⽂件夹的画我们要进去便利,不是⽂件夹那就输出即可,我们设置⼀个变量progress_value表⽰进度条的值,每便利⼀次就加1,然后通过线程类来暂停300毫秒,以便达到⾁眼能看到进度条变化的情况,注
意时间不能太⼩,不然看不到,嗖的⼀下就变成100了。然后将当前遍历到的⽂件或者⽂件夹路经加到⽂本框⾥,注意加上换⾏符,当然还有另⼀种加⼊⽅式。append(const QString &text)
1.9.槽函数
(1)QObject::connect(this->agree,SIGNAL(clicked()),this,SLOT(changeNextbutton()));
void Widget::changeNextbutton()
{
if(!this->agree->isChecked()){
this->setupWizard->button(QWizard::NextButton)->setEnabled(false);
}
else{
this->setupWizard->button(QWizard::NextButton)->setEnabled(true);
}
}
如果按钮没有被勾选,则设置安装向导对话框的下⼀步按钮为不可点击,否则设置为可以点击。
(2)QObject::connect(chosse_location,SIGNAL(clicked()),this,SLOT(chooseLocation()));
void Widget::chooseLocation()
{
QString locationname = QFileDialog::getExistingDirectory(this,"选择安装路径","/");
location->setText(locationname);
}
按钮被点击后打开⽂件对话框,这⾥有很多学问,我也不了解,这个是搜来的。
(3)QObject::connect(activation_mode,SIGNAL(currentIndexChanged(int)),this,SLOT(changeActivation(int)));
void Widget::changeActivation(int index)
{
if(0== index){
this->activate_widget->setCurrentWidget(this->serialnumber_widget);
}
else{
this->activate_widget->setCurrentWidget(this->enter_widget);
}
}
当前勾选的位置改变,作出响应,只有序列号和登录账号两种⽅式,所以index是0和1,0就显⽰序列号界⾯,1就显⽰登陆账号页⾯。(4)QObject::connect(this->setupWizard,SIGNAL(currentIdChanged(int)),this,SLOT(changePage(int)));
void Widget::changePage(int id)
{
if(id ==2){
findFile(this->location->text());
this->progress->setValue(this->progress->maximum());
}
}
对应的是向导对话框的页⾯转换消息,发出的消息中的int变量的值是,切换到的页⾯的id(0开始),2是第三页,到第三页时进⾏⽂件夹遍历。
2.源⽂件
#include"widget.h"
#include"ui_widget.h"
#include<QGridLayout>
#include<QDebug>
#include<QFile>
#include<QGroupBox>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QRadioButton>
#include<QFileDialog>
#include<QLabel>
#include<QLine>
#include<QFrame>
#include<QThread>
#include<QTransform>
Widget::Widget(QWidget *parent)
:QWidget(parent)
,ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("安装向导对话框");
this->setMinimumSize(300,200);
this->setMaximumSize(300,200);
beginbutton =new QPushButton("开始进⾏安装",this);
beginbutton =new QPushButton("开始进⾏安装",this);
beginbutton->move(50,50);
QObject::connect(beginbutton,SIGNAL(clicked()),this,SLOT(setupWizard_show()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::setupWizard_show()
{
this->setupWizard =new QWizard(this);
this->setupWizard->setWindowTitle("安装界⾯");
/
/设置风格和按钮名称
this->setupWizard->setButtonText(QWizard::BackButton,"上⼀步");
this->setupWizard->setButtonText(QWizard::NextButton,"下⼀步");
this->setupWizard->setButtonText(QWizard::FinishButton,"完成");
this->setupWizard->setButtonText(QWizard::CancelButton,"取消");
createPage_one();//创建第⼀个界⾯
createPage_two();//创建第⼆个界⾯
createPage_three();//创建第三个界⾯
createPage_four();//创建第四个界⾯
this->setupWizard->addPage(this->page_one);
this->setupWizard->addPage(this->page_two);
this->setupWizard->addPage(this->page_three);
this->setupWizard->addPage(this->page_four);
QObject::connect(this->setupWizard,SIGNAL(currentIdChanged(int)),this,SLOT(changePage(int)));
this->setupWizard->show();
this->setupWizard->button(QWizard::NextButton)->setEnabled(false);//显⽰之后将next按钮设置为不可点击,等待⽤户操作}
void Widget::createPage_one()
{
this->page_one =new QWizardPage;
this->page_one->setSubTitle("安装协议");
QTextEdit *protocl =new QTextEdit;
QFile *file =new QFile;
file->setFileName("D:/Qt_project/");
if(file->open(QIODevice::ReadOnly)){
QTextStream read(file);
protocl->adAll());
file->close();
delete file;
}
else{
protocl->setText("安装协议书");
}
this->agree =new QCheckBox("我已阅读⽤户保护协议,并同意这份协议。");
QObject::connect(this->agree,SIGNAL(clicked()),this,SLOT(changeNextbutton()));
QGridLayout* glayout =new QGridLayout;
glayout->addWidget(protocl,0,0);
glayout->addWidget(agree,1,0);
this->page_one->setLayout(glayout);
}
void Widget::createPage_two()
{
this->page_two =new QWizardPage;
this->page_two->setSubTitle("⽤户及兼容性设定");
QGroupBox* user =new QGroupBox("⽤户");
QHBoxLayout* hlayout =new QHBoxLayout;
QRadioButton* active_user =new QRadioButton("当前⽤户");
QRadioButton* all_users =new QRadioButton("所有⽤户");
hlayout->addWidget(active_user);
hlayout->addWidget(all_users);
user->setLayout(hlayout);
QGroupBox* compatibility =new QGroupBox("兼容性");
QVBoxLayout* vlayout =new QVBoxLayout;
QHBoxLayout* hlayout_one =new QHBoxLayout;
QHBoxLayout* hlayout_two =new QHBoxLayout;
QHBoxLayout* hlayout_three =new QHBoxLayout;
QCheckBox* checkbox_one =new QCheckBox("关联Doc、XLS、PPT");
QCheckBox* checkbox_two =new QCheckBox("关联PDF格式");
QCheckBox* checkbox_three =new QCheckBox("关联PNG、JPG等格式⽂件");
QCheckBox* checkbox_four =new QCheckBox("关联EPUBE");
location =new QLineEdit;
QPushButton* chosse_location =new QPushButton("安装位置");
QObject::connect(chosse_location,SIGNAL(clicked()),this,SLOT(chooseLocation()));
hlayout_one->addWidget(checkbox_one);
hlayout_one->addWidget(checkbox_two);
hlayout_two->addWidget(checkbox_three);
hlayout_two->addWidget(checkbox_four);
hlayout_three->addWidget(location);
hlayout_three->addWidget(chosse_location);
vlayout->addLayout(hlayout_one);
vlayout->addLayout(hlayout_two);
vlayout->addLayout(hlayout_three);
compatibility->setLayout(vlayout);
QGridLayout* glayout =new QGridLayout;
glayout->addWidget(user,0,0);
glayout->addWidget(compatibility,1,0);
this->page_two->setLayout(glayout);
}
void Widget::createPage_three()
{
this->page_three =new QWizardPage;
this->page_three->setSubTitle("安装进度");
QGridLayout* glayout =new QGridLayout;
glayout->addWidget(new QLabel("安装进度:"),0,0);
progress =new QProgressBar;
progress->setMaximum(100);progress->setMinimum(0);
progress->setValue(0);
this->progress_value =0;
glayout->addWidget(progress,0,1);
glayout->addWidget(new QLabel("安装⽂件列表:"),1,0);
filelist =new QTextEdit;
glayout->addWidget(filelist,2,0,1,2);
this->page_three->setLayout(glayout);
}
void Widget::createPage_four()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。