欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

QT 自定义模态对话框

程序员文章站 2022-07-08 19:59:24
新建一个MsgBox类 msgbox.h 代码 msgbox.cpp 代码 主函数调用代码: 目录构建图: 实现效果图: ......

新建一个msgbox类

msgbox.h 代码

#ifndef msgbox_h
#define msgbox_h

#include <qdialog>
#include <qpushbutton>
#include <qlabel>
#include <qmouseevent>
#include <qfont>
#include <qpixmap>
#include <qpainter>
#include<qbitmap>

class msgbox : public qdialog
{
    q_object

public:
    msgbox(qwidget *parent = 0);
    ~msgbox();

public:
    qpushbutton *ok_button;
    qpushbutton *close_button;
    qpushbutton *cancel_button;
    qlabel *ask_label;
    qlabel *msg_label;
    qlabel *title_label;
    qstring ok_text;
    qstring cancel_text;

public:
    void setinfo(qstring title_info, qstring info,qpixmap pixmap, bool is_ok_hidden,qstring language);
protected:
    qpoint move_point;
    bool mouse_press;

    void paintevent(qpaintevent *);
    void mousepressevent( qmouseevent * event );
    void mousereleaseevent( qmouseevent *);
    void mousemoveevent(qmouseevent *event);
public slots:
    void okoperate();
    void canceloperate();
    void closeoperate();
};

#endif // msgbox_h

msgbox.cpp 代码

#include "msgbox.h"

msgbox::msgbox(qwidget *parent)
    : qdialog(parent)
{
      this->resize(320, 160);

      //获取主界面的宽度
      int width = this->width();
      int height = this->height();

      //初始化为未按下鼠标左键
      mouse_press = false;

      //设置标题栏隐藏
      this->setwindowflags(qt::framelesswindowhint | qt::dialog);

      close_button = new qpushbutton(this);

      //close_button->loadpixmap(":/image/images/bg.png");
      close_button->setgeometry(width-30,5, 20, 20);
      close_button->setstylesheet("border-image:url(:/image/images/closebtn.png);");

      //设置标题
      title_label = new qlabel(this);
      title_label->setobjectname(qstring::fromutf8("labelone"));
      qfont font = title_label->font();
      font.setbold(true);
      title_label->setfont(font);
      title_label->setgeometry(0, 0, width-50, 30);

      //设置提示图片
      msg_label = new qlabel(this);
      msg_label->setgeometry(20, 50, 36, 36);
      msg_label->setscaledcontents(true);

      //设置提示信息,让qlabel能够自动判断并换行显示:
      ask_label = new qlabel(this);
      ask_label->setgeometry(65, 60, width-50, 25*2);
      ask_label->setwordwrap(true);
      ask_label->setalignment(qt::aligntop);

      cancel_button = new qpushbutton(this);
      cancel_button->resize(70, 25);
      cancel_button->move(width - cancel_button->width() - 10, height - 35);

      ok_button = new qpushbutton(this);
      ok_button->resize(70, 25);
      ok_button->move(width - ok_button->width() - cancel_button->width() - 20, height - 35);


      ok_button->setobjectname(qstring::fromutf8("pushbuttontwo"));
      cancel_button->setobjectname(qstring::fromutf8("pushbuttontwo"));

      qobject::connect(ok_button, signal(clicked()), this, slot(okoperate()));
      qobject::connect(close_button, signal(clicked()), this, slot(closeoperate()));
      qobject::connect(cancel_button, signal(clicked()), this, slot(canceloperate()));
}

msgbox::~msgbox()
{

}

//设置对话框信息
void msgbox::setinfo(qstring title_info, qstring info,qpixmap pixmap,bool is_ok_hidden,qstring language)
{
  title_label->settext(qstring("  ") + title_info);

  //设置提示信息
  ask_label->settext(info);
  msg_label->setpixmap(pixmap);

  //是否隐藏确定按钮
  ok_button->sethidden(is_ok_hidden);
  if(is_ok_hidden)
  {
      if(language == "中文")
      {
          cancel_button->settext(tr("确定"));
      }
      else if(language == "英文")
      {
          cancel_button->settext(tr("ok"));
      }
      else if(language == "泰文")
      {
          cancel_button->settext(tr("แน่ใจว่า"));
      }
  }
  else
  {
      if(language == "中文")
      {
          ok_button->settext(tr("确定"));
          cancel_button->settext(tr("取消"));
      }
      else if(language == "英文")
      {
          ok_button->settext(tr("ok"));
          cancel_button->settext(tr("cancel"));
      }
      else if(language == "泰文")
      {
          ok_button->settext(tr("แน่ใจว่า"));
          cancel_button->settext(tr("การยกเลิก"));
      }
  }

  //设置默认按钮为取消按钮
  cancel_button->setfocus();
}

void msgbox::paintevent(qpaintevent *)
{
  qpainter painter(this);
  painter.drawpixmap(rect(), qpixmap(":/image/images/close.png"));

  qbitmap bitmap(this->size());
  qpainter painter2(&bitmap);
  painter2.fillrect(bitmap.rect(), qt::white);
  painter2.setbrush(qcolor(0, 0, 0));
  painter2.drawroundedrect(rect(), 4, 4);
  setmask(bitmap);
}

void msgbox::mousepressevent( qmouseevent * event )
{
  //只能是鼠标左键移动和改变大小
  if(event->button() == qt::leftbutton)
  {
    mouse_press = true;
  }

  //窗口移动距离
  move_point = event->globalpos() - pos();
}

void msgbox::mousereleaseevent( qmouseevent *)
{
  mouse_press = false;
}

void msgbox::mousemoveevent(qmouseevent *event)
{
  //移动窗口
  if(mouse_press)
  {
    qpoint move_pos = event->globalpos();
    move(move_pos - move_point);
  }
}

//确认操作
void msgbox::okoperate()
{
  this->accept();
}

//取消操作
void msgbox::canceloperate()
{
  this->reject();
}

//关闭窗体操作
void msgbox::closeoperate()
{
    close();
}

主函数调用代码:

#include "msgbox.h"
#include <qapplication>

int main(int argc, char *argv[])
{
    qapplication a(argc, argv);
    msgbox w;

    w.setinfo(qstring("提示:"),qstring("是否关机?"),qpixmap(":/image/images/tip.png"),false,qstring("中文"));
    w.show();
    return a.exec();
}

目录构建图:

QT 自定义模态对话框

实现效果图:

QT 自定义模态对话框