QT20190312总结

时间:2019-03-12
本文章向大家介绍QT20190312总结,主要包括QT20190312总结使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
  • QAbstractListModel
  • QCompleter
//效果,当输入worldList这些单词首字母,会自动补全,且可通过Up和Down来选择补全的单词
QStringList wordList;
wordList << "alpha" << "omega" << "omicron" << "zeta";

QLineEdit *lineEdit = new QLineEdit(this);

QCompleter *completer = new QCompleter(wordList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);

lineEdit->setCompleter(completer);


- QCompleter的模型提供

QStringList strList;
wordList << "alpha" << "omega" << "omicron" << "zeta";

QCompleter *completer = new QCompleter(this);
QStringListModel *model = new QStringListModel(strList,completer);
completer->setModel(model);

//如果需要更新
static_cast<QStringListModel *>(completer->completer()->model())->setStringList(strList);
  • QDateEdit
  • QKeyEvent
//QKeyEvent可以得到键盘上任何键(组合按键不在此描述)
void HintList::keyPressEvent(QKeyEvent *ev)
{
    if(ev->key() == Qt::Key_Down)
    {
        this->setCurrentRow(this->currentRow() + 1);
        timer->stop();
        timer->start();
    }
    else if(ev->key() == Qt::Key_Up)
    {
        this->setCurrentRow(this->currentRow() - 1);
        timer->stop();
        timer->start();
    }
    else if(ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return)
    {
        emit sigKeyEnter(this->currentItem()->text());
        this->close();
    }
}
  • QListView
  • QListWidgetItem
  • QModelIndex
QModelIndex类定位数据在模型中的位置(必须有行和列,可能有父索引).
可以通过索引访问views、delegate和selection模型中的数据.
  • QPlainEdit
  • QScrollBar
QScrollBar的一个完整样式记录:
QScrollBar:vertical//首先是设置主体
{
    margin:30px 0px 30px 0px;
    background-color:#F0F1EF;
    border: 0px;
    width:30px;    
}
QScrollBar::handle:vertical//滑块
{
    background-color:#bbbbbb;
    width:30px;
    border-radius:7px;//圆角
}
QScrollBar::handle:vertical:hover
{
    background-color:#9B9B9B;
    width:30px;
    border-radius:7px;
}
QScrollBar::add-line:vertical//为滚动条下面的箭头区域
{
    subcontrol-origin: margin;
    border:1px solid rgb(240,241,239);
    height:30px;
}

QScrollBar::sub-line:vertical//为滚动条上面的箭头区域
{
   subcontrol-origin: margin;
    border:1px solid rgb(240,241,239);
    height:30px;
}
QScrollBar::add-page:vertical//表示未滑过的槽部分
{
  background-color:rgb(240,241,239);
}

QScrollBar::sub-page:vertical //表示已滑过的槽部分
{
    background-color:rgb(240,241,239); 
}
QScrollBar::up-arrow:vertical//箭头
{
  border:0px;
  width:30px;
  height:30px;
  border-image:url(":/Icons/up_Arrow.png");
}

QScrollBar::up-arrow:vertical:pressed
{
    border:0px;
    width:30px;
    height:30px;
    border-image:url(":/Icons/up_Arrow_down.png");
}
QScrollBar::down-arrow:vertical
{
    border:0px;
    width:30px;
    height:30px;
    border-image:url(":/Icons/down_Arrow.png");
}

QScrollBar::down-arrow:vertical:pressed 
{
    border:0px;
    width:30px;
    height:30px;
    border-image:url(":/Icons/down_Arrow_down.png");
}
  • QStatusBar
QStatusBar *statusBar = new QStatusBar;

statusBar->setStyleSheet(QString("QString::item{border: 0px}");   //设置无边框
  • QStringListModel
QStringListModel类提供一个字符串数据模型给views.
  • QTabBar
  • QTabWidget
  • QTreeWidgetItem
- 如下(ui界面下常见,继承QMainWindow):
	QTreeWidgetItem *item = new QTreeWidgetItem(this);
  • QTreeWidget

	//建立一个TreeWidget
	QTreeWidget *treeWidget = new QTreeWidget(this);
	//设置treeWidget的列数
	treeWidget->setColumnCount(3);
	
	//给Qt添加项目(不是头部,任何项目可以设置为头部),在这里添加项目可以说是添加一级节点
	{
		QStringList strList;
		strList << "1" << "2";
		QTreeWidgetItem *item = new QTreeWidgetItem(strList);
		treeWidget->addTopLevelItem(treeWidget);              //添加的项目位于原来项目末项下方

		//删除节点(这个item完全被销毁)
		delete item;
		
		//删除节点
		QTreeWidgetItem *item = treeWidget->takeTopLevelItem(int index);
		delete item;
	}
	
	//设置给QTreeWidget添加右键菜单功能
	{
		treeWidget->setContextMenuPolicy(Qt:;CustomContextMenu);
		connect(treeWidget,&QTreeWidget::customContextMenuRequested,[=](QPoint point){
			QMenu *menu = new QMenu(treeWidget);
			menu->setAttribute(Qt::WA_DeleteOnClose);
			QAction *action = new QAction("设置密码",menu);
			menu->addAction(action);
			
			QModeIndex index = treeWidget->indexAt(point);
			if(index.isValid())
			{
				menu->exec(QCursor::pos());
			}
		});
	}
	
	//得到当前是第几列(一般结合右键菜单功能使用)
	{
		int num = treeWidget->currentColumn();
		
		//得到当前项目(如果选中的有多行,那么当前行是最上层行
		QTreeWidgetItem *item = treeWidget->currentItem();
		
		//得到当前项目by视点
		QTreeWidgetItem *item = treeWidget->itemAt(QPoint(point));
		
		//得到当前所有选中项目
		QList<QTreeWidgetItem *> itemList = treeWidget->selectedItems();
		itemList.at(i)->text(0);
	}
	
	
	//设置item的某列可编辑(默认是不可编辑的)
	{
		treeWidget->openPersistentEditor(item,2);
		
		//关闭item的列可编辑
		treeWidget->closePersistentEditor(item,2);
	}
	
	//设置编辑项目的某一列(如果item的第二列(代号A)是可编辑的,那么显示时将全选A中文字并高亮
	{
		treeWidget->eidtItem(item,2);
	}
	
	
	//获取头部项目
	{
		treeWidget->headerItem();
	}
	
	//设置widget到treeWidget中的cell中(会覆盖先前文字和ICON)
	{
		treeWidget->setItemWidget(item,0,widget);
		
		//移除widget
		treeWidget->removeItemWidget(item,0);
	}
	
	//设置不自动排序
	{
		treeWidget->setSortingEnabeld(false);
	}
	
	//设置不可拖放
	{
		treeWidget->setAutoScroll(true);
	}
  • QValidator
QValidator类是一个验证输入框内容的验证器
它含有QRegExpValidator子类

	QLineEdit *lineEdit = new QLineEdit(this);
    QRegExpValidator *regValidator = new QRegExpValidator(QRegExp("[0-9]+$"),lineEdit);
    lineEdit->setValidator(regValidator);
	//对comBox测试时发现
	- comBox第一次显示时不接受任何键(包括数字、删除按键),重新选择另外一条语句才接受按键输入
	- comBox接受输入后,如果第一个字符是数字,那么验证器起作用,否则无效(此时删除按键起作用)

  • QWidget

  • QT网址

https://blog.csdn.net/Rebirthme/article/details/50402082
http://www.szsi.gov.cn/sbjxxgk/tzgg/simtgg/201807/t20180704_13524526.htm
https://blog.csdn.net/a568478312/article/details/79195218
https://blog.csdn.net/dpsying/article/details/78321602
https://www.cnblogs.com/qnkk123/p/6840944.html
https://libzip.org/documentation/zip_name_locate.html
https://msdn.microsoft.com/zh-cn/library/gg615407


http://www.cnblogs.com/doubleeleven/articles/3894250.html
https://blog.csdn.net/ziwenson/article/details/78389887
https://blog.csdn.net/ziwenson/article/details/78389887
https://blog.csdn.net/qq_35488967/article/details/76407785
https://blog.csdn.net/hudfang/article/details/44855795
https://blog.csdn.net/liusanping456/article/details/74990395
https://zhidao.baidu.com/question/1925438245605225147.html
https://bbs.csdn.net/topics/390375047?page=1
https://bbs.csdn.net/topics/310057592
https://blog.csdn.net/sunboyhch/article/details/8846206
https://blog.csdn.net/u011415782/article/details/78505422
https://www.jianshu.com/p/670de4f63689
https://www.cnblogs.com/zhoujgssp/p/7782278.html
https://blog.csdn.net/l_andy/article/details/79406035
https://blog.csdn.net/mingzznet/article/details/9418629
https://blog.csdn.net/fan_xingwang/article/details/80971157
https://blog.csdn.net/xiezhongyuan07/article/details/80676047
https://blog.csdn.net/xiezhongyuan07/article/details/80675483
https://www.cnblogs.com/findumars/p/8196256.html
https://ask.csdn.net/questions/256212
https://zhidao.baidu.com/question/585242790.html
https://blog.csdn.net/groundhappy/article/details/52046591
https://blog.csdn.net/rl529014/article/details/51355968/
https://blog.csdn.net/envenler/article/details/8021873?utm_source=blogxgwz2
https://blog.csdn.net/liang19890820
https://blog.csdn.net/seanyxie/article/details/7025183
https://www.cnblogs.com/findumars/p/5176140.html
https://blog.csdn.net/zxh2075/article/details/50574741?utm_source=blogxgwz4
https://blog.csdn.net/qq78442761/article/details/81124375
https://blog.csdn.net/yxy654538632/article/details/49496561
  • 未处理QT
1> 对于同一个页面上的对象可以设置一个私有变量,这样它就可以与同一页面上其它对象进行交互了
2> 如果一个对象上还附着比较多的其它对象,那么将其作为一个小型页面,与父对象(包括父对象一下所有子对象)以外的对象进行交互时,建立与父对象的信号-信号关系




Qt 调试相关
1) dumpObjectTree();  //使用此函数,可以打印出子控件的信息
例如:
QDateEdit *dateEdit = new QDateEdit(this);
    dateEdit->dumpObjectTree();
	
QDateEdit:: 
    QLineEdit::qt_spinbox_lineedit 
        QWidgetLineControl:: 
    QValidator::qt_spinboxvalidator 
	
2) findChild
	QDateEdit *dateEdit = new QDateEdit;
    dateEdit->setParent(this);
    dateEdit->dumpObjectTree();
    QLineEdit *lineEdit = dateEdit->findChild<QLineEdit*>();
    lineEdit->setReadOnly(true);

	
	  //高亮方法一
//    {
//        int number = 1;
//        while(!in.atEnd())
//        {
//            QString lineStr = in.readLine();
//            bool flag = false;
//            for(unsigned long i = 0; i < vectorLinePosition.size(); i++)
//            {
//                int lineNumber = vectorLinePosition.at(i).iline;

//                if(lineNumber == number)
//                {
//                    vector<HKeywordPos> vt_pos = vectorLinePosition.at(i).vt_pos;

//                    int strBegin = 0;
//                    QString htmlStr;
//                    for(unsigned long j = 0; j < vt_pos.size(); j++)
//                    {
//                        int begin = vt_pos.at(j).miPosBegin;
//                        int end = vt_pos.at(j).miPosEnd;

//                        htmlStr += QString("<font color = \"darkorange\">%1</font>").arg(lineStr.mid(strBegin, begin - strBegin));
//                        htmlStr += QString("<font color = \"blue\">%1</font>").arg(lineStr.mid(begin, end - begin));
//                        strBegin = end;
//                    }
//                    htmlStr += QString("<font color = \"darkorange\">%1</font>").arg(lineStr.mid(strBegin, lineStr.size() - 1));
//                    this->previewPlainText->appendHtml(htmlStr);
//                    flag = true;
//                }
//            }
//            if(!flag)
//            {
//                this->previewPlainText->appendHtml(lineStr);
//            }
//            number++;
//        }
//    }

    //高亮方法二
//    {
//        HTakeTime take("HighlightFildf111");
//        this->previewPlainText->setPlainText(in.readAll());
//        QTextCursor textCursor = this->previewPlainText->textCursor();
//        textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
//        int currentLine = textCursor.blockNumber();
//        QTextCharFormat lineFormat = this->previewPlainText->currentCharFormat();
//        lineFormat.setBackground(QColor("#FFC125"));
//        QTextCharFormat wordFormat = lineFormat;
//        wordFormat.setBackground(QColor("#00FFFF"));
//        HTakeTime take2("HighlightFildf111");
//        for(unsigned long i = 0; i < vectorLinePosition.size(); i++)
//        {
//            int lineNumber = vectorLinePosition.at(i).iline;
//            textCursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, lineNumber - currentLine -1);
//            currentLine = textCursor.blockNumber();
//            textCursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
//            textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
//            textCursor.mergeCharFormat(lineFormat);
//            this->previewPlainText->mergeCurrentCharFormat(lineFormat);
//            vector<HKeywordPos> vt_pos = vectorLinePosition.at(i).vt_pos;
//            for(unsigned j = 0; j < vt_pos.size(); j++)
//            {
//                int begin = vt_pos.at(j).miPosBegin;
//                int end = vt_pos.at(j).miPosEnd;
//                textCursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
//                textCursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, begin);
//                textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, end - begin);
//                textCursor.mergeCharFormat(wordFormat);
//                this->previewPlainText->mergeCurrentCharFormat(wordFormat);
//            }
//        }
//    }

	//高亮方法三,加高亮器

  • 组件运用
1、搜索框的运用
searchline.h

#ifndef SEARCHLINE_H
#define SEARCHLINE_H

#include <QWidget>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>

class SearchLine : public QLineEdit
{
    Q_OBJECT
public:
    explicit SearchLine(QWidget *parent = nullptr);

signals:

public slots:
};

#endif // SEARCHLINE_H


searchline.cpp

#include "searchline.h"

SearchLine::SearchLine(QWidget *parent) : QLineEdit(parent)
{
    QHBoxLayout *mainLayout = new QHBoxLayout;
    QPushButton *serchBtn = new QPushButton;
    serchBtn->setFixedSize(13,13);
    serchBtn->setCursor(Qt::PointingHandCursor);
    serchBtn->setToolTip(tr("搜索"));
    connect(serchBtn,&QPushButton::clicked,[=](){
        qDebug()<<"heljl";

    });

    this->setPlaceholderText(tr("搜索"));
    mainLayout->addWidget(serchBtn);
    mainLayout->addStretch();
    mainLayout->setContentsMargins(8,0,0,0);

    this->setTextMargins(13+8+2,0,0,0);
    this->setContentsMargins(0,0,0,0);
    this->setLayout(mainLayout);
    this->move(100,50);
    this->setStyleSheet("height:29px;border:1px solid #eaeaea;border-radius:14px;");
}



//通过转移焦点来使键盘响应事件被另一个对象所处理
if(ev->key() == Qt::Key_Down)
    {
        if(this->focusWidget() == this->lineEdit)
        {
            hintList->setFocus();
            hintList->setCurrentRow(0);
        }
    }
  • 编程规范
文件注释
/***********************************************
* 公司名称:
* 文件名  :
* 作者    :
* 目的    :
* 历史记录: //记录修改时间,修改内容,修改者
***********************************************/



函数注释
/***********************************************
* 函数名  :
* 功能描述:
* 输入参数:
* 输出参数:
* 返回值  :
* 算法说明:
***********************************************/