Qt 编程点点滴滴

一、如何是qt窗口运行后在右下角显示?

大致思想是先获取屏幕分辨率,减去窗口本身分辨率,移动到计算出的初始坐标。

1     QDesktopWidget* desktopWidget = QApplication::desktop();
2     QRect deskRect = desktopWidget->availableGeometry();
3     move( deskRect.width()-this->width()-12, deskRect.height()-this->height()-35);

 

 二、如何在当前窗口上显示标准对话框?

1     if(ui->lineEdit_IP->text().isEmpty())
2     {
3         QMessageBox::information(this, tr("提示"), tr("IP地址不能为空!"));
4     }

 

三、如何在textBrowser文本输出框中换行显示?

采用append即可,append带自动换行功能

1     ui->textBrowser_show->append("close socket!");

 

四、如何比较两个字符串?

QString提供了完美的接口。。。

1     if(QString::compare(data, "3333") == 0)
2     {
3         ui->pushButton_answer->setEnabled(true);
4     }

 

五、如何隐藏windows标题栏?

1 this->setWindowFlags(Qt::FramelessWindowHint); //隐藏windows标题栏

 

六、自定义窗体后如何响应鼠标拖动事件?

 1 //新窗口响应鼠标拖动事件
 2 void Widget::mousePressEvent(QMouseEvent *e)
 3 {
 4     if(isMinimized())
 5     {
 6         this->showNormal();
 7         qDebug()<<"now is in isminized";
 8     }
 9     last = e->globalPos();
10     qDebug()<<"qiut out ....\n";
11 }
12 
13 void Widget::mouseMoveEvent(QMouseEvent *e)
14 {
15      if(e->buttons()== Qt::LeftButton)
16      {
17           QPoint newpos = e->globalPos();
18           QPoint upleft = mapToParent(newpos - last); //计算距原位置的偏移
19           move(upleft);
20           last = newpos; //更新原位置到最新的位置
21      }
22 }

 

七、自定义窗口后如何响应最小化、关闭按钮?

 1 //自定义关闭窗口
 2 void Widget::mybuttonclose_clicked()
 3 {
 4     //方法一
 5     //QApplication* app;
 6     //app->quit();
 7 
 8     //方法二
 9     this->close();
10 }
11 
12 //窗口最小化
13 void Widget::mybuttonmini_clicked()
14 {
15     this->showMinimized();
16 }

 

posted @ 2012-11-13 12:08  24K.纯帅  阅读(304)  评论(0编辑  收藏  举报