模拟系统提示框

在FormCreate设置PersonSignLB的windows消息

aHint :=THintWindow.Create(nil);
aHint.Color :=clInfoBk;
FWndMethod := PersonSignLB.WindowProc;
PersonSignLB.WindowProc := LabelWndProc;

 

显示个人提示框

procedure TMainForm.LabelWndProc(var Msg: TMessage);

  //根据一定长度,将字符串变为回车的字符
  function SplitByLen(src:string;var row:Integer;perCount:Integer):string;
  var
    i,j:Integer;
    c:Char;
    sTemp,s1:string;
    bIsDBCS:Boolean;
  begin
    row :=Ceil(Length(src)/perCount);
    sTemp :='';
    bIsDBCS :=False;
    for I := 1 to row do
    begin
      for   j:= 1 to  perCount   do
      begin
        if bIsDBCS   then
          bIsDBCS   :=   False
        else
          if Windows.IsDBCSLeadByte(byte(src[j]))   then
            bIsDBCS   := True;
      end;

      if  bIsDBCS   then   Dec(perCount);
      if i=row then
        sTemp :=sTemp +Copy(src,1,perCount)
      else
        sTemp :=sTemp +Copy(src,1,perCount)+#10#13;
      src :=Copy(src,perCount+1,Length(src)-perCount);
    end;
    if bIsDBCS then
      Inc(perCount);
    Result :=sTemp;  
  end;

var
  p,p1,p2: TPoint;
  iRow:Integer;
  s:string;
begin
  GetCursorPos(p);
  p1 :=Point(p.x,p.y+20);

  if Msg.Msg = CM_MOUSELEAVE then
  begin
    ShowWindow(aHint.Handle, SW_HIDE);
  end
  else if Msg.Msg = CM_MOUSEENTER then
  begin
    s :=SplitByLen(FSingLB,iRow,36);
    //djc 2012-8-30 mod
    if iRow=1 then
      p2 :=Point(p.x+220,p.y+36)
    else
      p2 :=Point(p.x+220,p.y+26*iRow);
    aHint.ActivateHint(Rect(p1,p2),s);
  end;
  FWndMethod(Msg);
end;

 

 

模拟系统登录框,需要用到 Qt 中的 QDialog 和 QLineEdit 控件,以及数据库连接和操作。实现步骤如下: 1. 创建登录对话框 LoginDialog,并在该对话框中添加两个 QLineEdit 控件,一个用于输入用户名,一个用于输入密码,还需要添加两个 QPushButton 控件,一个用于登录,一个用于退出。 2. 在登录按钮的 clicked() 信号槽函数中,获取用户名和密码,然后根据数据库中的用户信息进行验证。如果验证成功,则关闭登录对话框,否则弹出提示框提示用户名或密码错误。 3. 在退出按钮的 clicked() 信号槽函数中,关闭登录对话框。 下面是示例代码: login_dialog.h: ```cpp #ifndef LOGINDIALOG_H #define LOGINDIALOG_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QSqlDatabase> class LoginDialog : public QDialog { Q_OBJECT public: explicit LoginDialog(QWidget *parent = nullptr); private: QLabel *m_userLabel; QLabel *m_pwdLabel; QLineEdit *m_userEdit; QLineEdit *m_pwdEdit; QPushButton *m_loginBtn; QPushButton *m_exitBtn; QSqlDatabase m_db; private slots: void login(); void exit(); }; #endif // LOGINDIALOG_H ``` login_dialog.cpp: ```cpp #include "login_dialog.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QMessageBox> #include <QSqlQuery> #include <QSqlError> LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("Login")); m_userLabel = new QLabel(tr("Username:")); m_pwdLabel = new QLabel(tr("Password:")); m_userEdit = new QLineEdit; m_pwdEdit = new QLineEdit; m_pwdEdit->setEchoMode(QLineEdit::Password); m_loginBtn = new QPushButton(tr("Login")); m_exitBtn = new QPushButton(tr("Exit")); QHBoxLayout *userLayout = new QHBoxLayout; userLayout->addWidget(m_userLabel); userLayout->addWidget(m_userEdit); QHBoxLayout *pwdLayout = new QHBoxLayout; pwdLayout->addWidget(m_pwdLabel); pwdLayout->addWidget(m_pwdEdit); QHBoxLayout *btnLayout = new QHBoxLayout; btnLayout->addWidget(m_loginBtn); btnLayout->addWidget(m_exitBtn); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(userLayout); mainLayout->addLayout(pwdLayout); mainLayout->addLayout(btnLayout); setLayout(mainLayout); connect(m_loginBtn, SIGNAL(clicked()), this, SLOT(login())); connect(m_exitBtn, SIGNAL(clicked()), this, SLOT(exit())); // 连接数据库 m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName("users.db"); if (!m_db.open()) { QMessageBox::critical(this, tr("Error"), tr("Failed to connect to database.")); } } void LoginDialog::login() { QString username = m_userEdit->text(); QString password = m_pwdEdit->text(); QSqlQuery query(m_db); QString sql = QString("SELECT * FROM users WHERE username='%1' AND password='%2'").arg(username).arg(password); if (query.exec(sql) && query.next()) { accept(); } else { QMessageBox::warning(this, tr("Warning"), tr("Username or password incorrect.")); } } void LoginDialog::exit() { reject(); } ``` 在这个示例中,我们利用了 Qt 的信号槽机制来响应登录和退出按钮的点击事件,利用了 Qt 的 QSqlDatabase 和 QSqlQuery 类来连接和操作数据库。当用户点击登录按钮时,我们将获取到的用户名和密码与数据库中的用户信息进行验证,如果验证成功,则调用 QDialog 的 accept() 函数关闭对话框并返回 QDialog::Accepted,否则弹出一个提示框提示用户名或密码错误。当用户点击退出按钮时,我们调用 QDialog 的 reject() 函数关闭对话框并返回 QDialog::Rejected。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值