QT草稿-记录有的没的

这篇博客介绍了在Qt中如何使用QMessageBox进行关闭软件的警告和询问操作,并展示了如何处理线程中断请求。此外,还涉及到图像处理线程的控制,包括线程的启动、停止以及栈大小的管理。在代码中,通过QThread和QMutex实现了线程间的同步,确保了线程安全。同时,讨论了Qt事件循环在多线程环境中的重要性,特别是在长时间运行的任务中避免线程过早退出的问题。
摘要由CSDN通过智能技术生成
int ret =QMessageBox::warning(this, "关闭软件", "<font size='26' color='red'>是否关闭软件?</font>",
                                  QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape);



int ret = QMessageBox::question(this, "关闭软件", "是否关闭软件?",QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape);
if(!CGlobalValue::m_pProcesImgThread[0]->isInterruptionRequested())
 CGlobalValue::m_pProcesImgThread[i]->requestInterruption();
#include <QMutexLocker> 
QMutexLocker lock(&m_mutex);

private:
    QMutex m_mutex;
void ImageCaptureProcs::msleep(int ms)
{
    QTime dieTime = QTime::currentTime().addMSecs(ms);

    while (QTime::currentTime() < dieTime)
//       // qDebug() << "Date:" << QDate::currentDate();
        QCoreApplication::processEvents(QEventLoop::AllEvents, 50);   //  把我害惨了-3天了 一直关不了线程
}
int camID =0;
QString str= QString("相机 %1 断开").arg(camID+1);
this->groupBox->setStyleSheet("background-color:rgba(0,0,0,0); border-image: url(:/picture/Frame.png);color:rgb(81,192,223);");

void MainWindow::Log_Mess(QString str  )
{
    QIcon aIcon;
    aIcon.addFile(":/images/icon/check2.ico");        //设定图标
    QListWidgetItem* aItem=new QListWidgetItem(str);  //创建一个Item
    aItem->setIcon(aIcon);    //设置图标
    aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
//    if(colorNum == 0 )
//    {
//        ui->listWidget->setStyleSheet("color: rgb(0, 255, 0)");
//    }


    int cnt=ui->listWidget->count();  //项的个数
    if(cnt>100)
    {
        ui->listWidget->clear();      //清除所以
    }

    ui->listWidget->addItem(aItem);    //增加一个项
    ui->listWidget->scrollToBottom();  //定位到最后一个


    // 文本输出结束后自动换行
//    ui->textBrowser->append(str);
//    ui->textBrowser->moveCursor(QTextCursor::End);

}
void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);  //这个宏用于在函数中定义不在函数体里使用的参数
#if 0
    QPainter painter(this);  //画形状
    painter.setRenderHint(QPainter::Antialiasing);
#if 0

    QPen pen;  //画笔的颜色等属性
    pen.setColor((QColor(255,152,0)));
    pen.setWidth(10);
    pen.setStyle(Qt::DashDotLine);
    painter.setPen(pen);
    painter.drawLine(0,0,width(),height());
#endif
    //QBrush brush=QBrush(QColor(0,0,255));  //区域填充
    //painter.setBrush(brush);

    QLinearGradient linear = QLinearGradient(150,150,450,150);  //渐变色
    linear.setColorAt(0,Qt::green);
    linear.setColorAt(1,Qt::red);
    painter.setBrush(linear);
    painter.setPen(Qt::NoPen);
    painter.drawRect(150,150,300,300);
#endif
}
//打开图片
void SetVar::openPic(Mylabel *m_mylabel,HTuple& HalconID,HObject &CurrentImg,QLabel *label_status)
{
    HTuple FileNameModel;
    QString path = QFileDialog::getOpenFileName(this,"Select Image","","(*.bmp *.png *.jpg)");
    if(path == "") return;
    //Qstring转HTuple
    FileNameModel = HTuple(path.toLatin1().data());
    ReadImage(&CurrentImg,FileNameModel);
    showImg(m_mylabel,HalconID,CurrentImg,label_status);
   // DispObj(halconImage,WindowHandle);


//    QString path = QFileDialog::getOpenFileName(this, "加载图像", "./", "图像文件(*.bmp *.png *.jpg)");
//    QFileInfo fileInfo(path);
//    QString dir = fileInfo.path();
//    if (!path.isEmpty())
//    {
//        ListFiles(dir.toStdString().c_str(), "files", &imgFiles);
//        TupleRegexpSelect(imgFiles, HTuple("\\.bmp|png|jpg").Append("ignore_case"), &imgFiles);
//        int i;
//        for ( i = 0; i < imgFiles.Length(); i++)
//        {
//            QString currentPath = imgFiles[i];
//            currentPath.replace("\\", "/");
//            if (currentPath == path)
//            {
//                ReadImage(&CurrentImg, imgFiles[i]);
//                showImg(m_mylabel,HalconID,CurrentImg,label_status);
//            }
//        }
//    }
}
//打开图片
void SetVar::openPic(Mylabel *m_mylabel,HTuple& HalconID,HObject &CurrentImg,QLabel *label_status)
{
    HTuple FileNameModel;
    QString path = QFileDialog::getOpenFileName(this, "加载图像", "./", "图像文件(*.bmp *.png *.jpg)");
    if(path == "") return;
    //Qstring转HTuple
    FileNameModel = path.toStdString().c_str();
    ReadImage(&CurrentImg,FileNameModel);
    showImg(m_mylabel,HalconID,CurrentImg,label_status);
}
 int thread_id=GetCurrentThreadId();
    HTuple thread_str=thread_id;
    CGlobalValue::message_thred[0][2]="处理图像线程0: "+thread_str;

    set_display_font( CGlobalValue::m_hWinHandle[0],16, "mono", "false", "false" );
    for (int i = 0; i < 3; ++i) {
        disp_message(CGlobalValue::m_hWinHandle[0],CGlobalValue::message_thred[0][i],"image",i*100+10,5,"cyan","false");
    }

Qstring转int

int triggerTime = str_trigger_time.toInt();  //  QString转int



 QString str = QString::number(5);  //  int转QString

 运行和停止

void MainWindow::on_bnt_start_stop_clicked()
{
    if (flag_satrt)
    {
        ui->bnt_start_stop->setStyleSheet("border-image: url(:/images/icon/停止.png)");  //不要有空格,否则不显示图标
        ui->lab_start_stop->setText("停止");
        flag_satrt = 0;
softTrigger_timer->stop();
        for (int i = 0; i < CAM_NUM; ++i)
        {
            if(camera[i]->IsOpenCamera() )
            {

                camera[i]->StopCamera();   //停止相机采集
                QThread::usleep(100000);
            }
        }
    }
    else
    {
        ui->bnt_start_stop->setStyleSheet("border-image: url(:/images/icon/启动.png)");
        ui->lab_start_stop->setText("启动");
        flag_satrt = 1;
 softTrigger_timer->start(CGlobalValue:: m_soft_trigger_once_time);
        for (int i = 0; i < CAM_NUM; ++i)
        {
            if(camera[i]->IsOpenCamera() )
            {
                camera[i]->StartCamera();   //开启相机采集
                QThread::usleep(100000);

            }
        }
    }
}

一般在 Windows 的栈大小为2M,在 Linux 下是8M。

      Qt 提供了获取以及设置栈空间大小的函数:stackSize()、setStackSize(uint stackSize)。
      其中 stackSize() 函数不是返回当前所在线程的栈大小,而是获取用 stackSize() 函数手动设置的栈大小。如果是用编译器默认的栈大小,该函数返回0,这一点需要注意。为什么要设置栈的大小?这是因为有时候我们的局部变量很大(常见于数组),当超过编译器默认大小时程序就会因为栈溢出而报错,这时候就需要手动设置栈大小了。

默认 run() 函数会调用 exec() 函数,即启用一个局部的不占 CPU 的事件循环。为什么要默认启动个事件循环呢?这是因为没有事件循环的话,耗时代码只要执行完线程就会退出,频繁的开销线程显然很浪费资源。
    因此,如果使用上述第二种“开线程”的方式,别忘了在 run() 函数中调用 exec() 函数。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值