• Qt记录之QTabWidget的使用


    记录一下QTabWidget的一次使用

    QTabWidget实现出来效果就是标签页的界面,点击一个标签,主界面就展现那个标签页的东西。

    值得注意的就是以下几点:

    1.QTabWidget::addTab(Widget,"title"),增加一个标签页。比如这个Widget为QPushButton,那个增加的标签页一整页就是一个按钮。

    2.QTabWidget::setLayout(Layout),这样设置,所有标签页也是这个布局了,要是Layout上有Widget,那么所有标签页都展示Layout上的Widget了。

    3.例子里面,第一个标签页是Widget就是一个新建的QWidget类,要添加组件,就给这个Widget设置一个Layout,然后添加Widget到Layout上即可。第二个标签页是QListWidget,会自己读取目录,然后把目录中的文件,文件夹展示出来。

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QWidget>
    #include <QTabWidget>
    #include<QListWidgetItem>
    #include<QListWidget>
    #include<QDir>
    #include<QEvent>
    #include<QMenu>
    #include<QFileSystemWatcher>
    #include<QInputDialog>
    #include<QDesktopServices>
    #include<iostream>
    #include<QApplication>
    #include<QDesktopWidget>
    #include<QLabel>
    #include<QToolButton>
    #include<QPushButton>
    #include<QGridLayout>
    #include<QGroupBox>
    #include<QLineEdit>
    #include<QListWidget>
    #include<QListWidgetItem>
    #include<QFileInfoList>
    #include<QDir>
    #include<QDebug>
    #include<QFileDialog>
    #include<QMessageBox>
    
    #define QS(x) QString::fromLocal8Bit((x))
    #define sQS(x) QString::fromLocal8Bit((x).data())
    #define toStr(x) string(x.toLocal8Bit())
    #define qsTQS(x) QUrl::fromLocalFile(x).toString()//.remove(0,8)
    #define qsurlTQS(x) QUrl::fromLocalFile(x).url().remove(0,8)
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
    private slots:
        void chooseDir();
        void chooseDirFromCurrentDir();
        void finishCommit();
        void slotDirShow(QListWidgetItem*);
        void renameFile();
        void deleteFile();
        void copyFile();
        void openFile();
        void showFiledir();
    
    private:
        QTabWidget* m_tabUI;
        bool m_showTab1;
        bool m_showTab2;
        bool m_showTab3;
        bool m_showTab4;
        bool m_showTab5;
        QListWidget* file;
        QDir* dir;
        QMenu* menu;
        QFileSystemWatcher* watcher;
    
    protected:
        bool Widget::eventFilter(QObject *watched, QEvent *event);
    };
    #endif // WIDGET_H
    
    
    #include "widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        this->setWindowTitle("Tools");
        this->setWindowFlags(Qt::Widget|Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
        const int w = 400, h = 350;
        const int x = (QApplication::desktop()->width() - w) >> 1;
        const int y = (QApplication::desktop()->height() - h) >> 1;
        this->setGeometry(x, y, w+40, h+20);
    
        m_tabUI = new QTabWidget(this);
        m_tabUI->setGeometry(x-460,y-200,w,h);
        m_tabUI->show();
        m_showTab1 = true;
        m_showTab2 = false;
        m_showTab3 = false;
        m_showTab4 = false;
        m_showTab5 = true;
        if(m_showTab1)
        {
            QWidget* ui2Trans = new QWidget();
    
            m_tabUI->addTab(ui2Trans,QS("Tab1"));
    
    
            QGridLayout* mainLayout1 = new QGridLayout();
    
    
            QGroupBox* box1 = new QGroupBox(QS("box1"));
            QGridLayout* layout1 = new QGridLayout();
            QLabel* label = new QLabel(QS("请选择文件:"));
            QLineEdit* lineEdit = new QLineEdit();
            QPushButton* pushButton_open = new QPushButton(QS("打开目录"));
            connect(pushButton_open,SIGNAL(clicked(bool)),this,SLOT(chooseDir()));
            QPushButton* pushButton_choose = new QPushButton(QS("选择"));
            connect(pushButton_choose,SIGNAL(clicked(bool)),this,SLOT(chooseDirFromCurrentDir()));
            QPushButton* pushButton_trans = new QPushButton(QS("信息框"));
            connect(pushButton_trans,SIGNAL(clicked(bool)),this,SLOT(finishCommit()));
    
            layout1->addWidget(label,0,0);
            layout1->addWidget(lineEdit,0,1);
            layout1->addWidget(pushButton_open,0,3);
            layout1->addWidget(pushButton_choose,0,5);
            layout1->addWidget(pushButton_trans,0,7);
    
    
            box1->setLayout(layout1);
            mainLayout1->addWidget(box1,0,0);
    
    
            QGroupBox* box2 = new QGroupBox(QS("box2"));
            QGridLayout* layout2 = new QGridLayout();
            QLabel* label_2 = new QLabel(QS("请选择文件:"));
            QLineEdit* lineEdit2 = new QLineEdit();
            QPushButton* pushButton_open2 = new QPushButton(QS("打开目录"));
            connect(pushButton_open2,SIGNAL(clicked(bool)),this,SLOT(chooseDir()));
            QPushButton* pushButton_choose2 = new QPushButton(QS("选择"));
            connect(pushButton_choose2,SIGNAL(clicked(bool)),this,SLOT(chooseDirFromCurrentDir()));
            QPushButton* pushButton_trans2 = new QPushButton(QS("信息框"));
            connect(pushButton_trans2,SIGNAL(clicked(bool)),this,SLOT(finishCommit()));
    
            layout2->addWidget(label_2,0,0);
            layout2->addWidget(lineEdit2,0,1);
            layout2->addWidget(pushButton_open2,0,3);
            layout2->addWidget(pushButton_choose2,0,5);
            layout2->addWidget(pushButton_trans2,0,7);
    
    
            box2->setLayout(layout2);
            mainLayout1->addWidget(box2,1,0);
    
    
    
            QGroupBox* box3 = new QGroupBox(QS("box3"));
            QGridLayout* layout3 = new QGridLayout();
            QLabel* label_3 = new QLabel(QS("请选择文件:"));
            QLineEdit* lineEdit3 = new QLineEdit();
            QPushButton* pushButton_open3 = new QPushButton(QS("打开目录"));
            connect(pushButton_open3,SIGNAL(clicked(bool)),this,SLOT(chooseDir()));
            QPushButton* pushButton_choose3 = new QPushButton(QS("选择"));
            connect(pushButton_choose3,SIGNAL(clicked(bool)),this,SLOT(chooseDirFromCurrentDir()));
            QPushButton* pushButton_trans3 = new QPushButton(QS("信息框"));
            connect(pushButton_trans3,SIGNAL(clicked(bool)),this,SLOT(finishCommit()));
    
            layout3->addWidget(label_3,0,0);
            layout3->addWidget(lineEdit3,0,1);
            layout3->addWidget(pushButton_open3,0,3);
            layout3->addWidget(pushButton_choose3,0,5);
            layout3->addWidget(pushButton_trans3,0,7);
    
    
            box3->setLayout(layout3);
            mainLayout1->addWidget(box3,2,0);
    
    
            QGroupBox* box4 = new QGroupBox(QS("box4"));
            QGridLayout* layout4 = new QGridLayout();
            QLabel* label_4 = new QLabel(QS("请选择文件:"));
            QLineEdit* lineEdit4 = new QLineEdit();
            QPushButton* pushButton_open4 = new QPushButton(QS("打开目录"));
            connect(pushButton_open4,SIGNAL(clicked(bool)),this,SLOT(chooseDir()));
            QPushButton* pushButton_choose4 = new QPushButton(QS("选择"));
            connect(pushButton_choose4,SIGNAL(clicked(bool)),this,SLOT(chooseDirFromCurrentDir()));
            QPushButton* pushButton_trans4 = new QPushButton(QS("信息框"));
            connect(pushButton_trans4,SIGNAL(clicked(bool)),this,SLOT(finishCommit()));
    
    
            layout4->addWidget(label_4,0,0);
            layout4->addWidget(lineEdit4,0,1);
            layout4->addWidget(pushButton_open4,0,3);
            layout4->addWidget(pushButton_choose4,0,5);
            layout4->addWidget(pushButton_trans4,0,7);
    
    
            box4->setLayout(layout4);
            mainLayout1->addWidget(box4,3,0);
    
            ui2Trans->setLayout(mainLayout1);
        }
    
        if(m_showTab5)
        {
            file = new QListWidget();
            m_tabUI->addTab(file,QS("文件浏览"));
    
            dir = new QDir();
            qDebug()<<dir->path()<<endl;
            showFiledir();
            file->installEventFilter(this);
            connect(file,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(slotDirShow(QListWidgetItem*)));
            menu = new QMenu(this);
            QAction *rename = menu->addAction(QS("重命名此文件"));
            connect(rename,SIGNAL(triggered(bool)), this, SLOT(renameFile()));
            QAction *deletefile= menu->addAction(QS("删除文件"));
            connect(deletefile,SIGNAL(triggered(bool)), this,SLOT(deleteFile()));
            QAction* open = menu->addAction(QS("打开文件"));
            connect(open,SIGNAL(triggered(bool)), this, SLOT(openFile()));
            watcher = new QFileSystemWatcher(this);
            watcher->addPath(dir->path());
            connect(watcher,SIGNAL(directoryChanged(QString)),this,SLOT(showFiledir()));
            QAction* copy = menu->addAction(QS("复制文件"));
            connect(copy,SIGNAL(triggered(bool)), this, SLOT(copyFile()));
    
        }
    
    }
    
    Widget::~Widget()
    {
    }
    
    
    void Widget::chooseDir()
    {
    //   QString path = QFileDialog::getExistingDirectory(this,"choose exe directory.","D:/");
        QString path = QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("打开exe文件"),"D:/","exe files(*.exe)");
    
       qDebug()<<path<<endl;
    }
    
    void Widget::chooseDirFromCurrentDir()
    {
    //    QString path = QFileDialog::getExistingDirectory(this,"choose cpp directory.","./");//打开文件夹用这个
    
        //打开文件用下面这个
        QString path = QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("打开cpp文件"),"/","cpp files(*.cpp)");
        qDebug()<<path<<endl;
    }
    
    void Widget::finishCommit()
    {
        QMessageBox::information(NULL,QS("提示"),QS("这是一个信息框!"),QMessageBox::Ok);
    }
    
    void Widget::slotDirShow(QListWidgetItem * item)
    {
        qDebug()<<item->whatsThis()<<endl;
        if(item->whatsThis()=="file")
        {
            openFile();
            return;
        }
        else if(item->whatsThis()!="dir")
        {
            return;
        }
    //    return;
        qDebug()<<"dir = "<<item->text()<<endl;
        watcher->removePath(dir->path());//去掉之前路径的监视
        dir->cd(item->text());
        qDebug()<<dir->path()<<endl;
        watcher->addPath(dir->path());//添加新路径的监视
        showFiledir();//切换以后,重新展示即可
    }
    
    void Widget::renameFile()
    {
        bool ok = false;
        QInputDialog* dialog = new QInputDialog();
        dialog->setFixedSize(350,250);
        dialog->setWindowTitle(QS("提示"));
    //    dialog->setGeometry(100,100,100,100);
        dialog->setLabelText(QS("请输入新的文件名:"));
        dialog->setOkButtonText(QS("确认"));
        dialog->setCancelButtonText(QS("取消"));
    
        //前面设置的属性,一用getText就没用了。
    
        QString filename = dialog->getText(NULL, QS("提示"),QS("请输入新的文件名:"), QLineEdit::Normal,0, &ok);
    
        if(filename.isEmpty())
        {
            return;
        }
        if(file->currentItem()->whatsThis()=="dir")
        {
            QString path = dir->path() + "/" + file->currentItem()->text();
            qDebug()<<QS("重命名文件: ")<<path<<endl;
            QString newpath = dir->path() + "/" + filename;
            dir->rename(path,newpath);
        }
        else if(file->currentItem()->whatsThis()=="file")
        {
            QString path = dir->path() + "/" + file->currentItem()->text();
            qDebug()<<QS("重命名文件: ")<<path<<endl;
            QString newpath = dir->path() + "/" + filename;
            dir->rename(path,newpath);
        }
    
    }
    
    void Widget::deleteFile()
    {
        QString path = dir->path() + "/" + file->currentItem()->text();
        qDebug()<<QS("删除文件: ")<<path<<endl;
        if(file->currentItem()->whatsThis()=="dir")
        {
            dir->rmpath(path);
        }
        else if(file->currentItem()->whatsThis()=="file")
        {
            dir->remove(path);
        }
    
    }
    
    void Widget::copyFile()
    {
        bool ok = false;
        QInputDialog* dialog = new QInputDialog();
        dialog->setFixedSize(350,250);
        dialog->setWindowTitle(QS("提示"));
    //    dialog->setGeometry(100,100,100,100);
        dialog->setLabelText(QS("请输入新的文件名:"));
        dialog->setOkButtonText(QS("确认"));
        dialog->setCancelButtonText(QS("取消"));
    //    qDebug()<<dialog->textValue()<<endl;
    
        QString filename = dialog->getText(NULL, QS("提示"),QS("请输入新的文件名:"), QLineEdit::Normal,0, &ok);
    
        if(filename.isEmpty())
        {
            return;
        }
        if(file->currentItem()->whatsThis()=="dir")
        {
            //文件夹复制需要多个文件,暂不处理
        }
        else if(file->currentItem()->whatsThis()=="file")
        {
            QString path = dir->path() + "/" + file->currentItem()->text();
            qDebug()<<QS("复制文件: ")<<path<<endl;
            QString newpath = dir->path() + "/" + filename;
            QFile::copy(path,newpath);
        }
    
    }
    
    void Widget::openFile()
    {
        qDebug()<<QS("打开文件")<<endl;
        QString path = dir->path() + QS("/") + file->currentItem()->text();
        qDebug()<<path<<endl;
    //    QDesktopServices::openUrl(path);
    //    path = path.toLocal8Bit().constData();
    //    path = QString::fromStdWString(data);
        QDesktopServices::openUrl(qsTQS(path)); //中文文件打不开,就用这个宏定义就好了。
    
    }
    
    /*
     * 重新展示文件夹
     */
    void Widget::showFiledir()
    {
        file->clear();
        QFileInfoList list = dir->entryInfoList(QDir::AllEntries|QDir::NoDot);
        for(int i=0;i<list.count();i++)
        {
            QFileInfo tmpInfo = list.at(i);
            if(tmpInfo.isDir())
            {
                QIcon icon("dir.png");
                QString fileName = tmpInfo.fileName();
                QListWidgetItem *tmp = new QListWidgetItem(icon,fileName);
                tmp->setWhatsThis("dir");
                file->addItem(tmp);
            }
        }
        for(int i=0;i<list.count();i++)
        {
            QFileInfo tmpInfo = list.at(i);
            if(tmpInfo.isFile())
            {
                QIcon icon("file.png");
                QString fileName = tmpInfo.fileName();
                QListWidgetItem *tmp = new QListWidgetItem(icon,fileName);
                tmp->setWhatsThis("file");
                file->addItem(tmp);
            }
        }
    }
    
    
    bool Widget::eventFilter(QObject *watched, QEvent *event)
    {
        // 右键弹出菜单
        if (watched == (QObject*)file) {
            if (event->type() == QEvent::ContextMenu) {
                if(file->currentItem()!=NULL)//空白处不弹
                {
                    menu->exec(QCursor::pos());
                }
            }
        }
        return QObject::eventFilter(watched, event);
    }
    
    
  • 相关阅读:
    web前端工程师
    java工程师
    原因原来默认预检测会检测是否存在多选框
    软件测试&安全测试高峰论坛
    安卓学习图
    为什么mongo中不能用int作为key
    历经小半宿吧。哎,终于搭建好了Linux-C的环境
    把昨晚写的东西完善了一下,还行,真差不多
    半宿了,仿写了个CList模板类,留着以后用吧
    今天复习了一下完成端口网络模型
  • 原文地址:https://www.cnblogs.com/dayq/p/16024366.html
Copyright © 2020-2023  润新知