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() 函数。