記在前面: 以前剛開始的時候, 想實現某某功能, 跑百度谷歌, 查Qt助手, 好不容易實現目標功能; 但當時並沒有做任何的筆記; 或者只記錄幾個比較深刻的知識; 后來要再次實現那些功能, 又得重新花一大半時間去搜索查閱, 真真糾結悲催..@_@
目的: 就這樣子, 寫個備忘的筆記, 不時更新, 便於以后檢索查閱
內容:
1.視圖模型中, 設置視圖不可編輯 setEditTriggers(QAbstractItemView::NoEditTriggers);
2.對話框去掉右上角的問號: setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
3.對話框加上最小化按鈕: setWindowFlags(windowFlags()|Qt::WindowMinimizeButtonHint);
4.打開文件夾通用對話框:
QString dir= QFileDialog::getExistingDirectory(
this,tr("Open Directory"),
QString(),
QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks
);
5.打開文件對話框 --可多選:
QFileDialog fd; fd.setFilter("exefile(*.exe *.dll *.sys);;Allfile(*.*)"); //設置文件過濾器 QListView *listView = fd.findChild<QListView*>("listView"); if (listView) listView->setSelectionMode(QAbstractItemView::ExtendedSelection); if(fd.exec() == QDialog::Accepted) //如果成功的執行 { QSet<QString> pathSet; QString path; QStringList strList = modFileLists.stringList(); strList.append(fd.selectedFiles()); foreach(path, strList) { pathSet.insert(path); } strList.clear(); foreach(path, pathSet) { strList.append(path); } modFileLists.setStringList(strList); } else fd.close();
6.多語言:
1.pro工程文件里面添加 TRANSLATIONS+=CN.ts 2.選擇Qt Creator環境的菜單欄 工具->外部->Qt語言家->更新翻譯 3.桌面開始菜單里面Qt目錄打開 Linguist工具 4.Linguist工具加載生成好的CN.ts文件 5.填好翻譯, 保存, Release, 就生成好編譯后的qm文件 6.在工程的源文件中, 這樣加載qm文件: QTranslator translator; QLocale locale; if(QLocale::Chinese == locale.language()) {//中文環境 translator.load("Cn.qm"); //中文 a.installTranslator(&translator); }//否則默認用英文
7.自定義右鍵菜單:
1.widget對象設置 setContextMenuPolicy(Qt::CustomContextMenu) 2.創建一個QMenu對象, 指定好父類指針 3.關聯widget的customContextMenuRequested(QPoint)信號到自定義的槽 connect(ui->tbvFileLog, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(FileLogTbvPopupMenu(QPoint))); 4.在槽函數里面執行Qmenu對象的exec方法 exec(QCursor::pos()); 補充: QMenu菜單添加菜單項: QMenu對象調用addAction方法, 通過信號槽關聯這個QAction的點擊事件
8.item view控件設置可多選:
1.setSelectionMode(QAbstractItemView::MultiSelection); 2.setSelectionMode(QAbstractItemView::ExtendedSelection); 區別: 1.不用按ctrl鍵即可多選; 2按ctrl鍵多選
9.item view控件多選后刪除
QModelIndexList indexList = ui->listvFiles->selectionModel()->selectedRows(); QModelIndex index; int i = 0; foreach(index, indexList) { this->modFileLists.removeRow(index.row() - i); ++i; }
10.QByteArray存入中文時亂碼
A: QByteArray bytes; bytes.append(this->modFileLists.data(this->modFileLists.index(i), Qt::DisplayRole).toString()); //亂碼 B: QByteArray bytes; bytes.append(this->modFileLists.data(this->modFileLists.index(i), Qt::DisplayRole).toString().toLocal8Bit()); //正常
11.Item View控件設置屬性設置
setSelectionBehavior(QAbstractItemView::SelectRows); //選擇整行模式
horizontalHeader()->setStretchLastSection(true); //設置最后一列自動拉伸
verticalHeader()->hide(); //隱藏行號列
setAlternatingRowColors(true); //設置行顏色屬性, 比較美觀
12.QByteArray轉char*
調用QByteArray的data方法即可
13.QTableView改變被選擇上的項顏色, qss樣式語法:
#tbvXXX::item:selected { background-color: rgb(170, 0, 0); }
//把QTableView控件的焦點去掉, 這樣選擇的時候就不會有那個難看的虛框
//QTableView對象調用setFocusPolicy(Qt::NoFocus);
14.Qt托盤
//使用QSystemTrayIcon類 QSystemTrayIcon *tray; //托盤 QMenu *meuTray; //托盤菜單 QAction *acTrayQuit; //托盤退出 this->tray = new QSystemTrayIcon(this); this->meuTray = new QMenu(this); this->acTrayQuit = this->meuTray->addAction(QIcon(":/res/image/quit.png"), tr("Quit")); connect(this->acTrayQuit, SIGNAL(triggered()), this, SLOT(OnExit())); this->tray->setContextMenu(this->meuTray); this->tray->setIcon(QIcon(":/res/image/tray.ico")); this->tray->show(); connect(this->tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(OnTrayActivated(QSystemTrayIcon::ActivationReason)));
voidUpdateTerminal::OnTrayActivated(QSystemTrayIcon::ActivationReasonreason)
{
switch(reason)
{
caseQSystemTrayIcon::DoubleClick:
if(this->isHidden())
this->show();
break;
}
}