Qt Widget应用案例实现是本文要介绍的内容,文章中主要讲解了自制的两个Qt Widget应用,具体内容的实现来看本文详解。Qt自带的Widget不大够用了,于是和他一起做了两个。一个是图片按钮。这是一个基于QAbstractButton的按钮,按钮的实体是图片,coyotte508实现的时候忽略了按钮被按下时的效果,我给他完善了一下。
- 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聊天输入框模式的一个输入框,输入框对光标-上和光标-下两个键盘输入产生反应,能够回到之前输入的内容或者返回当前内容。
- 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应用案例实现的内容介绍完了,希望通过Qt Widget应用内容的学习能对你有所帮助!