19閱讀網  |  最近更新  |  TAG  | 
19閱讀網
當前位置:首頁 > qt界面設計

qt界面設計

來源:互聯網 時間:2017-07-21 閱讀: 手機版

篇一:qt界面設計

1. 子窗體或控件,重寫paintEvent事件,畫邊框、背景、文字、圖片效果比較好。用paintEvent 事件畫邊框,用QPalette設置背景。http://blog.csdn.net/imxiangzi/article/details/50154951/

// 畫邊框http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setRenderHint(QPainter::Antialiasing, true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setPen(QPen(QColor(225,70,56), 2));http://blog.csdn.net/imxiangzi/article/details/50154951/

//  painter.setBrush(QColor(16,62,81));  // 也可設置背景,有時效果不好http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawRoundedRect(rect(), 5.0, 5.0);http://blog.csdn.net/imxiangzi/article/details/50154951/

// 設置背景http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setBrush(QPalette::Window, QColor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setPalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  setAutoFillBackground(true); 繼承QDialog,想讓對話框以模態運行,使用exec(),繼承時使用setWindowFlags()后則達不到模態的效果。不知道原因。 QStringList 輸出http://blog.csdn.net/imxiangzi/article/details/50154951/

QStringList m_strLang English http://blog.csdn.net/imxiangzi/article/details/50154951/

  Chinese http://blog.csdn.net/imxiangzi/article/details/50154951/

qDebug( set language: %s , m_strLang.at(0).toLocal8Bit().constData()); QStringList 使用注意http://blog.csdn.net/imxiangzi/article/details/50154951/

  QStringList strDateformatList;http://blog.csdn.net/imxiangzi/article/details/50154951/

  strDateformatListhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_strMonth - m_strDay - m_strYearhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_strDay - m_strMonth - m_strYearhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_strYear - m_strMonth - m_strDayhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  ;http://blog.csdn.net/imxiangzi/article/details/50154951/

如果strDateformatList 在生命周期內多次調用,則會多次插入字符串,使得字符串重復,這不是我們想要的。那就定義為局部變量,每次會重新賦 ,達到更新的目的。需要動態重新的頁面可以在showEvent函數中重新。 用paintEvent或用調色板為QWidget填充背景時,需要加上setAutoFillBackground(true);語句,否則沒效果。 想使彈出的窗口為圓角,用paintEvent畫圓角矩形,但四個直角仍然存在,不美觀。可以把背景設為透明,用paintEvent畫出想要的背景,因為四個角沒有畫上背景,是透明的,所以看不見。http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 設置背景為透明 (構造函數中)http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setBrush(QPalette::Window, QColor(255, 255, 255, 0));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setPalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  setAutoFillBackground(true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 畫背景和邊框 (paintEvent函數中)http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setRenderHint(QPainter::Antialiasing, true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setPen(QPen(QColor(225,70,56), 2));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setBrush(QColor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawRoundedRect(rect(), 5.0, 5.0); Qt::Key_Enter、Qt::Key_Returehttp://blog.csdn.net/imxiangzi/article/details/50154951/

Qt::Key_Enter對應小鍵盤的Enter鍵http://blog.csdn.net/imxiangzi/article/details/50154951/

Qt::Key_Return對應大鍵盤的Enter鍵 事件過濾。窗口部件的事件到達目的地之前提前獲取并處理相關事件,實現個性化的處理。http://blog.csdn.net/imxiangzi/article/details/50154951/

我在QLabel對象中放入兩個QPushButton對象和兩個QLabel對象,QPushButton實現 的調節,其中一個QLabel對象用于顯示 ,一個QLabel對象用于顯示標題。類 SpinBox。在多個SpinBox上點擊實現焦點切換,當鼠標點到QPushButton上時,QPushButton處理了mousePressEvent事件,所以無法實現焦點切換。為兩個QPushButton對象安裝事件過濾器,然后在其父部件中處理eventFilter函數http://blog.csdn.net/imxiangzi/article/details/50154951/

m_btnLeft- installEventFilter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

m_btnRight- installEventFilter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

//安裝事件過濾器后,傳給m_btnLeft、m_btnRight的事件會首先發送給eventFilter函數http://blog.csdn.net/imxiangzi/article/details/50154951/

bool ESpinBoxItem::eventFilter(QObject*obj,QEvent*ev)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

if(obj == m_btnLeft || obj == m_btnRight)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

if(ev- type() == QEvent::MouseButtonPress)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

emit sign_spinBoxPressed(m_nItem);http://blog.csdn.net/imxiangzi/article/details/50154951/

return true; // 不傳給目標對象處理http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

return false; // 不處理的事件則傳給目標對象處理http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

10. 拍照動畫http://blog.csdn.net/imxiangzi/article/details/50154951/

使用QTimeLine和QGraphicsPixmapItem對象,當時間線幀改變時,設置QGraphicsPixmapItem的位置,實現動畫效果。http://blog.csdn.net/imxiangzi/article/details/50154951/

// 在QGraphicsView構造函數中http://blog.csdn.net/imxiangzi/article/details/50154951/

// take photo animationhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timerAnimation = new QTimeLine(900);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timerAnimation- setCurveShape(QTimeLine::LinearCurve);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timerAnimation- setFrameRange(0, 240);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_pixmapUpAnimation = QPixmap( :/images/animation.png http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_pixmapDownAnimation = QPixmap( :/images/animation.png http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem = new QGraphicsPixmapItem;http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem = new QGraphicsPixmapItem;http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- setZValue(4);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setZValue(4);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- setPos(0, 0);  // 左上角為原點http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setPos(0, 240);http://blog.csdn.net/imxiangzi/article/details/50154951/

  scene()- addItem(m_upPixmapItem);http://blog.csdn.net/imxiangzi/article/details/50154951/

  scene()- addItem(m_downPixmapItem);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- hide();http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- hide();http://blog.csdn.net/imxiangzi/article/details/50154951/

  connect(m_timerAnimation, SIGNAL(frameChanged(int)), this, SLOT(slot_playAnimation(int)));http://blog.csdn.net/imxiangzi/article/details/50154951/

// 槽函數http://blog.csdn.net/imxiangzi/article/details/50154951/

void EGraphicsView::slot_playAnimation(int nFrame)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- show();http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- show();http://blog.csdn.net/imxiangzi/article/details/50154951/

  qDebug( nFrame: %d , nFrame);http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(nFrame 120)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- setPixmap(m_pixmapUpAnimation.scaled(320, nFrame));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setPixmap(m_pixmapDownAnimation.scaled(320, nFrame));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setPos(0, 240 - nFrame);http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  elsehttp://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_upPixmapItem- setPixmap(m_pixmapUpAnimation.scaled(320, 240 - nFrame));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setPixmap(m_pixmapDownAnimation.scaled(320, 240 - nFrame));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downPixmapItem- setPos(0, nFrame);http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

11.在移動設備上使用漸變或使用漸變的圖片時,會出現條紋狀,達不到效果。在初始化QApplication對像前調用下面的語句則可以達到效果:http://blog.csdn.net/imxiangzi/article/details/50154951/

QApplication::setColorSpec(QApplication::ManyColor); //the right choice for applications that use thousands of colorshttp://blog.csdn.net/imxiangzi/article/details/50154951/

QApplication app(argc, argv,  QApplication::GuiServer );http://blog.csdn.net/imxiangzi/article/details/50154951/

12.用setStyleSheet設置背景圖片http://blog.csdn.net/imxiangzi/article/details/50154951/

m_labelButton- setStyleSheet( background-image: url(:images/shutdown/slider.png); background-repeat: no-repeat; //注意,不加background-repeat: no-repeat可能會出現透視,很丑。QLabel對象可用setPixmap設置象素圖。http://blog.csdn.net/imxiangzi/article/details/50154951/

13.使對話框圓角http://blog.csdn.net/imxiangzi/article/details/50154951/

  //構造函數中http://blog.csdn.net/imxiangzi/article/details/50154951/

  setAutoFillBackground(true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setColor(QPalette::Window, QColor(0, 0, 0, 0));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setPalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  //paintEvent函數中http://blog.csdn.net/imxiangzi/article/details/50154951/

  QPainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setRenderHint(QPainter::Antialiasing,true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setPen(QPen(Qt::red,2));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setBrush(QColor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawRoundedRect(rect(),5.0,5.0);http://blog.csdn.net/imxiangzi/article/details/50154951/

// 圓角處有雜色,原因不祥http://blog.csdn.net/imxiangzi/article/details/50154951/

14. 對某些默認事件的處理修改http://blog.csdn.net/imxiangzi/article/details/50154951/

正常情況下,按下Tab鍵,被QWidget看成是去移動鍵盤焦點,但少數窗口部件需要自行解釋。http://blog.csdn.net/imxiangzi/article/details/50154951/

bool MyClass::event(QEvent *e)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(e- type() == QEvent::KeyPress)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  QKeyEvent *ke = (QKeyEvent *)e;http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(ke- key() == Qt::Key_Tab)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  //這里是特定的Tab處理http://blog.csdn.net/imxiangzi/article/details/50154951/

  k- accept();http://blog.csdn.net/imxiangzi/article/details/50154951/

  return TRUE;http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  else if(e- type()  = QEvent::User)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 這里是自定義事件處理http://blog.csdn.net/imxiangzi/article/details/50154951/

  return TRUE;http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  QWidget::event(e);http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

  15.QLabel繼承QFrame,有QFrame的特性。如setFrameStyle(int)可設置QLabel的FrameStyle(框架類型)http://blog.csdn.net/imxiangzi/article/details/50154951/

FrameStyle有兩種:http://blog.csdn.net/imxiangzi/article/details/50154951/

a.第一種是frameshape:http://blog.csdn.net/imxiangzi/article/details/50154951/

有以下幾種類型:http://blog.csdn.net/imxiangzi/article/details/50154951/

Constanthttp://blog.csdn.net/imxiangzi/article/details/50154951/

Valuehttp://blog.csdn.net/imxiangzi/article/details/50154951/

Descriptionhttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::NoFramehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws nothinghttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::Boxhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0001http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a box around its contentshttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::Panelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0002http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a panel to make the contents appear raised or sunkenhttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::StyledPanelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0006http://blog.csdn.net/imxiangzi/article/details/50154951/

draws a rectangular panel with a look that depends on the current GUI style. It can be raised or sunken.http://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::HLinehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0004http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a horizontal line that frames nothing (useful as separator)http://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::VLinehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0005http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a vertical line that frames nothing (useful as separator)http://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::WinPanelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0003http://blog.csdn.net/imxiangzi/article/details/50154951/

draws a rectangular panel that can be raised or sunken like those in Windows 95. Specifying this shape sets the line width to 2 pixels. WinPanel is provided for compatibility. For GUI style independence we recommend using StyledPanel instead.http://blog.csdn.net/imxiangzi/article/details/50154951/

b.第二種是frameshadowhttp://blog.csdn.net/imxiangzi/article/details/50154951/

有以下幾種類型:http://blog.csdn.net/imxiangzi/article/details/50154951/

Constanthttp://blog.csdn.net/imxiangzi/article/details/50154951/

Valuehttp://blog.csdn.net/imxiangzi/article/details/50154951/

Descriptionhttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::Plainhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0010http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear level with the surroundings; draws using the palettecolor (without any 3D effect)http://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::Raisedhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0020http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear raised; draws a 3D raised line using the light and dark colors of the current color grouphttp://blog.csdn.net/imxiangzi/article/details/50154951/

QFrame::Sunkenhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0030http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear sunken; draws a 3D sunken line using the light and dark colors of the current color grouphttp://blog.csdn.net/imxiangzi/article/details/50154951/

組合 的效果圖:http://blog.csdn.net/imxiangzi/article/details/50154951/

  lineEdit =new QLineEdit;

 label- setBuddy(lineEdit);

  caseCheckBox= new QCheckBox(tr("Match case"));

 backwardCheckBox = new QCheckBox(tr("Search backward"));

  finon =new QPushButton(tr(" Find"));

 finon- setDefault(true);

 finon- setEnabled(false);

  closeButton= new QPushButton(tr("Close"));

 connect(lineEdit, SIGNAL(textChanged(const QString )),

 this, SLOT(enableFinon(const QString )));

 connect(finon, SIGNAL(clicked()),

 this, SLOT(findClicked()));

 connect(closeButton, SIGNAL(clicked()),

 this, SLOT(close()));

  //使用布局管理器布局控件

  QHBoxLayout*topLeftLayout = new QHBoxLayout;

 topLeftLayout- addWidget(label);

 topLeftLayout- addWidget(lineEdit);

  QVBoxLayout*leftLayout = new QVBoxLayout;

 leftLayout- addLayout(topLeftLayout);

 leftLayout- addWidget(caseCheckBox);

 leftLayout- addWidget(backwardCheckBox);

  QVBoxLayout*rightLayout = new QVBoxLayout;

 rightLayout- addWidget(finon);

 rightLayout- addWidget(closeButton);

 rightLayout- addStretch();

  QHBoxLayout*mainLayout = new QHBoxLayout;

 mainLayout- addLayout(leftLayout);

 mainLayout- addLayout(rightLayout);

  //設置窗口的布局管理器

 setLayout(mainLayout); 

 setWindowTitle(tr("Find"));

 setFixedHeight(sizeHint().height());

}

使用Qt Designer設計界面 采用Qt Designer,使得快速創建對話框成為可能。在QtDesigner環境中,所有的操作都采用可視化的操作,可拖放控件、關聯信號與槽、設置特定控件的屬性。使用QtDesigner設計界面的方法如下圖所示:

 

猜你喜歡

最新文章

熱門文章

神龙宝石电子游艺 天天开心斗地主赚钱 固废处理赚钱吗 如何在淘宝卖视频赚钱吗 什么能够赚钱 长安欧诺跑货拉拉赚钱么 烧烤海鲜赚钱吗 方寸飞行符赚钱吗 种莫玉能赚钱 腾讯游戏赚钱收入排行榜2015年 手机下载东西就赚钱是真的吗 15年零成本赚钱快的加盟项目 电脑版gta5怎样赚钱 梦幻老区龙宫赚钱 百度推荐视频可以赚钱 阿里巴靠什么赚钱 财付通云币怎么赚钱