Qt設計時的一些技巧 (轉)

引言  最近在做數據庫相關課程設計,所以就藉此機會,先熟悉一下Qt的一些編程,同時瞭解一下C++的一些特性。其實更重要的是如何組織好相關模塊的連接,如何規劃項目,等等。所以就順道把過程中遇到的問題和重要的一些控件的槽和信號介紹一下,以後忘了可以回來看。呵呵。
   以下是我用到的一些重要的函數和代碼:
一、數據庫的連接
  1. QSqlDatabase TB = QSqlDatabase::addDatabase("QMYSQL");// becomes the new default connect
//TB.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");//使用SSL安全連接
TB.setHostName("127.0.0.1");//主機名
TB.setDatabaseName("chat");//數據庫名
//TB.setPort(3306);//端口號
TB.setUserName("root");//用戶名
TB.setPassword("123");//密碼
  2.數據的避免無法處理和插入漢字問題
create database chat character set GBK;//這樣建立數據庫就可以插入中文或者其它辦法use chat;--查看數據庫的字符集 show variables like 'character\_set\_%'; show variables like 'collation_%'; --設置數據庫字符編碼 set names 'GBK' --這樣的話在consle端口查看不會是亂碼--drop table xxxx;--修改系列--增加Head屬性alter table tb_user add Head varchar(20);--刪除數據delete from tb_userRS where friendID = "107865437";--更新屬性update tb_user set username="淼的世界" where id="642419907";

二、應用程序漢字處理(黑代碼)
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));//字體處理
三、設定整個對話框的背景顏色(黑代碼)
this->setAutoFillBackground(true);//1s step
QPalette palette;//2s step
palette.setBrush(QPalette::Background, QBrush(QPixmap("image/login.JPG")));
this->setPalette(palette);//3s step
四、應用程序圖標設計
圖標設計的時候,我們要把.ico文件放在工程的ico目錄下,或者其它目錄也行。然後在該圖標目錄下建立一個後綴爲.rc的文件,內容如下,IDI_ICON1 ICON DISCARDABLE "cool.ico" ,就樣在程序中如此調用:this->setWindowIcon(QIcon("ico/cool.ico"));//設置程序ico圖標,注意要在.pro做設置,OK。
五、應用程序enter快捷鍵事件的實現
1.在頭文件中定義一個快捷鍵 QShortcut *key_enter_login;//快捷鍵登錄
2.定義相應的槽 private slots:
void enter_longin();
3.構造函數中
key_enter_login = new QShortcut(QKeySequence(tr("Return")), this);//設置Enter快捷鍵,但是Enter不行,用Return就行。
connect(key_enter_login, SIGNAL(activated()), this, SLOT(enter_longin()));
六、文字鏈接到網頁
構造函數中
QLabel *m_r_acount = new QLabel(this);m_r_acount->setText(tr("<a href=\"http://www.yafeilinux.com\">註冊帳號 "));
connect(m_r_acount, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(const QString)));
槽中不要忘了頭文件申明槽)
void MyFirstQQ::openUrl(const QString &register_url)
{QDesktopServices::openUrl(QUrl("http://localhost:8080/chat/register.jsp"));
}
六、QComboBox的設置
QComboBox *m_a_choice = new QComboBox(this);
QStringList C_strings;
C_strings <<"666666"<< "642419907" << "767938089" << "107865437" << "110120119" ;
completer = new QCompleter(C_strings, this);//可以進行匹配的哦
m_a_choice->clear();
m_a_choice->addItems(C_strings);
m_a_choice->setMaxVisibleItems(7);//設置最大顯示下列項 超過要使用滾動條拖拉
m_a_choice->setEditable(true);
m_a_choice->setCompleter(completer);
六、鼠標滑過事件
頭文件中:
protected:
    void mouseMoveEvent(QMouseEvent *event);//鼠標滑動
構造函數中:
//設定鼠標跟蹤事件this->setMouseTracking(true);//標籤跟蹤qlabel->setMouseTracking(true);//還可以設置其它控件
實現函數中:(舉個小例子)
int x = event->x();int y = event->y();if(x > u_b_x && x < u_b_x+u_b_width && y > u_b_y && y < u_b_y+u_b_height){u_background->setPixmap(QPixmap("image/skin3.JPG"));}else if(x > u_button_x && x < u_button_x+u_button_width && y > u_button_y && y < u_button_y+u_button_height){textbox.show();}else{u_background->setPixmap(QPixmap("image/skin.JPG"));textbox.close();}
六、重寫標籤鼠標點擊事件的實現(需要重寫QLabel,構造自己的QLabel->MyLabel)
#ifndef CLICKEDLABEL_H_#define CLICKEDLABEL_H_#include <QtGui>class ClickedLabel : public QLabel      //繼承QLabel,重寫事件,實現點擊標籤退出{    Q_OBJECTpublic:    ClickedLabel(QWidget *parent = 0);    int MyLabelPressed;    void mousePressEvent(QMouseEvent *e);//添加鼠標響應事件    void mouseReleaseEvent(QMouseEvent *e);signals:    void clicked();//點擊信號};#endif /* CLICKEDLABEL_H_ */

#include "ClickedLabel.h"ClickedLabel::ClickedLabel(QWidget *parent):QLabel(parent){    MyLabelPressed = 0;}void ClickedLabel::mousePressEvent ( QMouseEvent * e ){   MyLabelPressed = 1;}void ClickedLabel::mouseReleaseEvent ( QMouseEvent * e ){    if (MyLabelPressed)    {        emit clicked();        MyLabelPressed = 0;    }}
實使用的時候,只要包括該頭文件,然後用ClickedLabel定義一個這樣的標籤,那麼在如下使用信號和槽:
ClickedLabel *s_exit;//退出標籤,這樣就可以使用clicked()信號了
connect(s_exit, SIGNAL(clicked()), this, SLOT(closed()));

七、重寫QListWidget鼠標右擊彈出菜單事件。(該實現需要重寫QListWidget)
#ifndef LISTWIDGET_H_#define LISTWIDGET_H_#include<QApplication>#include<QWidget>#include<QListWidget>#include<QMenu>#include<QAction>#include "scaninfo.h"#include "addfriend.h"class ListWidget : public QListWidget{ Q_OBJECTpublic: explicit ListWidget(QWidget *parent = 0);//explicit構造函數只能被顯示調用 void contextMenuEvent ( QContextMenuEvent * event );private: QAction *action;//刪除選項 QAction *scan_action;//查看資料 QAction *add_friend;//添加好友 QMenu *popMenu;};#endif /* LISTWIDGET_H_ */

#include "ListWidget.h"
#include<QMessageBox>

ListWidget::ListWidget(QWidget *parent):
QListWidget(parent)
{
action = new QAction(QIcon("image/delete.jpg"),tr("刪除好友"),this); //刪除事件
scan_action = new QAction(QIcon("image/scan.jpg"),tr("查看資料"),this);
add_friend = new QAction(QIcon("image/addFriend.jpg"),tr("添加好友"),this);
popMenu = new QMenu(this);
}

void ListWidget::contextMenuEvent ( QContextMenuEvent * event )
{
if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) //如果有item則選中
{
itemAt(mapFromGlobal(QCursor::pos()))->setSelected(true);
}
else
{
popMenu->addAction(add_friend);
popMenu->removeAction(action);
popMenu->removeAction(scan_action);
popMenu->exec(QCursor::pos());
}
if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) //如果有item則添加"刪除"菜單
{
popMenu->addAction(action);
popMenu->addAction(scan_action);
popMenu->removeAction(add_friend);
popMenu->exec(QCursor::pos()); // 菜單出現的位置爲當前鼠標的位置
}
}
使用的時候在你需要的類中(頭文件或者構造函數):ListWidget u_good_Widget = new ListWidget(this);就行

七、類似於QQ那種伸縮列表的小效果,自己可以參考
  1.申明該QListWidget的相關槽
connect(u_good_Widget, SIGNAL(clicked(QModelIndex)), this, SLOT(good_Stretch(QModelIndex)));
  2.槽實現
void UserQQ::good_Stretch(QModelIndex index){if(u_flag){u_item->setIcon(QIcon(QPixmap("image/QListWidgetItemUp.png")));}else
{u_item->setIcon((QPixmap("image/QListWidgetItemDown.png")));}if(index.row()==0){for(int j = 1; j < u_hide_count+1; j++){u_good_Widget->setRowHidden(j, u_flag);}u_flag = !u_flag; //這裏不要這樣做u_flag = false;結果也出乎意料}}
七、QMenu中QAction的事件定義
QMenu *cmenu = NULL;//定義並初始化右鍵cmenu = new QMenu(this);//初始化右鍵在何處顯示QAction *EnterGroupTalk = cmenu->addAction(QIcon("image/grouptalk.jpg"),tr("進入羣聊"));//添加右鍵菜單顯示項connect(EnterGroupTalk,SIGNAL(triggered()),this,SLOT(on_actionEnter_GroupTalk_triggered()));//使AddRecode與其他函數關聯
八、主窗體,標籤之類的一些外觀設計
this->setFixedSize(this->width(), this->height());//固定大小this->setWindowFlags(Qt::FramelessWindowHint);//去掉標題欄語句QFont font("ZYSong18030", 12);//設置字體this->setFont(font);//透明度this->setWindowOpacity(0.8);
//標籤
QLabel s_acount_label = new QLabel(this);
s_acount_label->setFont(QFont("ZYSong18030", 8));//字體小點
s_acount_label->setGeometry(155, 10, 60, 20);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章