詳解兩個自制Qt Widget應用案例實現(xiàn)
作者:佚名
Qt Widget應用案例實現(xiàn)是本文要介紹的內容,文章中主要講解了自制的兩個Qt Widget應用,具體內容的實現(xiàn)來看本文詳解。
Qt Widget應用案例實現(xiàn)是本文要介紹的內容,文章中主要講解了自制的兩個Qt Widget應用,具體內容的實現(xiàn)來看本文詳解。Qt自帶的Widget不大夠用了,于是和他一起做了兩個。一個是圖片按鈕。這是一個基于QAbstractButton的按鈕,按鈕的實體是圖片,coyotte508實現(xiàn)的時候忽略了按鈕被按下時的效果,我給他完善了一下。
- class QImageButtonP : public QAbstractButton
- {
- Q_OBJECT
- public:
- QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked ="");
- QSize sizeHint() const;
- QSize minimumSizeHint() const;
- QSize maximumSize() const;
- void changePics(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked = "");
- protected:
- void paintEvent(QPaintEvent *e);
- void mouseMoveEvent(QMouseEvent *e);
- void mousePressEvent(QMouseEvent *e);
- void mouseReleaseEvent(QMouseEvent *e);
- private:
- QPixmap myPic, myHoveredPic, myCheckedPic, myPressedPic;
- int lastUnderMouse; // last mouse pos recorded
- bool bpressed;
- enum State {
- Normal,
- Hovered,
- Checked,
- Pressed
- };
- int lastState;
- };
- QImageButtonP::QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked)
- : myPic(normal), myHoveredPic(hovered), myPressedPic(pressed), lastUnderMouse(-1), bpressed(false)
- {
- setFixedSize(myPic.size());
- #if defined(WIN32) || defined(WIN64)
- setMask(::mask(myPic));
- #endif
- lastState = Normal;
- /* Both are necessary for some styles */
- setMouseTracking(true);
- setAttribute(Qt::WA_Hover, true);
- if (checked != "")
- myCheckedPic = QPixmap(checked);
- }
- void QImageButtonP::changePics(const QString &normal, const QString &hovered,const QString &pressed, const QString &checked)
- {
- myPic = QPixmap(normal);
- myHoveredPic = QPixmap(hovered);
- myPressedPic = QPixmap(pressed);
- if (checked != "")
- myCheckedPic = QPixmap(checked);
- #if defined(WIN32) || defined(WIN64)
- setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) :
- (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));
- #endif
- update();
- }
- void QImageButtonP::mousePressEvent(QMouseEvent *e)
- {
- bpressed = true;
- QAbstractButton::mousePressEvent(e);
- }
- void QImageButtonP::mouseReleaseEvent(QMouseEvent *e)
- {
- bpressed = false;
- QAbstractButton::mouseReleaseEvent(e);
- }
- QSize QImageButtonP::sizeHint() const
- {
- return myPic.size();
- }
- QSize QImageButtonP::minimumSizeHint() const
- {
- return sizeHint();
- }
- QSize QImageButtonP::maximumSize() const
- {
- return sizeHint();
- }
- void QImageButtonP::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- int newState;
- if ((this->isChecked()) && !myCheckedPic.isNull()) {
- newState = Checked;
- painter.drawPixmap(e->rect(), myCheckedPic, e->rect());
- }else if(this->isDown () && !myPressedPic.isNull()) {
- newState = Pressed;
- painter.drawPixmap(e->rect(), myPressedPic, e->rect());
- }else {
- if (!underMouse()) {
- newState = Normal;
- painter.drawPixmap(e->rect(), myPic, e->rect());
- }
- else {
- newState = Hovered;
- painter.drawPixmap(e->rect(), myHoveredPic, e->rect());
- }
- }
- if (newState != lastState) {
- lastState = newState;
- #if defined(WIN32) || defined(WIN64)
- setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) :
- (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));
- #endif
- }
- lastUnderMouse = underMouse();
- }
- void QImageButtonP::mouseMoveEvent(QMouseEvent *)
- {
- if (int(underMouse()) == lastUnderMouse)
- return;
- update();
- }
另一個是IRC聊天輸入框模式的一個輸入框,輸入框對光標-上和光標-下兩個鍵盤輸入產(chǎn)生反應,能夠回到之前輸入的內容或者返回當前內容。
- class QIRCLineEdit : public QLineEdit
- {
- Q_OBJECT
- public:
- QIRCLineEdit();
- private:
- void keyPressEvent(QKeyEvent *);
- QList<QString> m_Inputlist1;//Stores the inputed strings,up list.
- QList<QString> m_Inputlist2;//Stores the inputed strings,down list.
- //QString m_Currentline;//Stores a copy of the current text in the LineEdit.
- public slots:
- void myTextEdited();
- void myclear();
- };
- QIRCLineEdit::QIRCLineEdit()
- {
- connect(this,SIGNAL(textEdited(QString)),this,SLOT(myTextEdited()));
- }
- void QIRCLineEdit::keyPressEvent(QKeyEvent *e)
- {
- if(e->key() == Qt::Key_Up) {
- if(text()==""){
- if(m_Inputlist1.empty()){
- }else{
- setText(m_Inputlist1.last());
- m_Inputlist1.removeLast();
- }
- }else if(text()!=""){
- m_Inputlist2.append(text());
- if(m_Inputlist1.empty()){
- clear();
- }else{
- setText(m_Inputlist1.last());
- m_Inputlist1.removeLast();
- }
- }
- }else if(e->key() == Qt::Key_Down) {
- if(text()==""){
- if(m_Inputlist2.empty()){
- }else{
- setText(m_Inputlist2.first());
- m_Inputlist2.removeFirst();
- }
- }else if(text()!=""){
- if(m_Inputlist2.empty()){
- m_Inputlist1.append(text());
- clear();
- }else{
- m_Inputlist1.append(text());
- setText(m_Inputlist2.first());
- m_Inputlist2.removeFirst();
- }
- }
- }else{
- QLineEdit::keyPressEvent(e);
- }
- }
- void QIRCLineEdit::myTextEdited()
- {
- //if(!m_Inputlist2.empty()) m_Inputlist1.append(m_Currentline);
- m_Inputlist2.clear();
- }
- void QIRCLineEdit::myclear()
- {
- if(text()!=""){
- m_Inputlist1.append(text());
- }
- QLineEdit::clear();
- }
小結:詳解兩個自制Qt Widget應用案例實現(xiàn)的內容介紹完了,希望通過Qt Widget應用內容的學習能對你有所幫助!
責任編輯:zhaolei
來源:
互聯(lián)網(wǎng)