0

これは私の btconnect.h ファイルです

#ifndef BTCONNECT_H
#define BTCONNECT_H

#include "scandialog.h"

namespace Ui {
class BTConnect;
}

class BTConnect : public QWidget
{
    Q_OBJECT

public:
    explicit BTConnect(QWidget *parent = 0);
    ~BTConnect();

private slots:
    void on_ScanButton_clicked();

    void ScanBTDevices();

    //some slots here

    void ScanDialogShow();

    void ScanDialogClose();

public slots:
//some slots here

private:
    Ui::BTConnect *ui;

    QProcess BTscan_Process;

    scanDialog *scan;
};

#endif // BTCONNECT_H

btconnect.cpp

BTConnect::BTConnect(QWidget *parent) :
QWidget(parent),
ui(new Ui::BTConnect)
{
    //set the userinterface as BTConnect.ui
    ui->setupUi(this);

    scan = new scanDialog(this);
}


void BTConnect::ScanDialogShow()
{
    scan->show();
}

void BTConnect::ScanDialogClose()
{
    scan->close();
}

void BTConnect::ScanBTDevices()
{
    ScanDialogShow();

    //Command to scan nearby bluetooth devices
    //"hcitool scan"
    QString cmd("hcitool scan");

    //start the process
    BTscan_Process.start(cmd);

    //Wait for the processs to finish with a timeout of 20 seconds
    if(BTscan_Process.waitForFinished(20000))
    {
        //Clear the list widget
        this->ui->listWidget->clear();

        //Read the command line output and store it in QString out
        QString out(BTscan_Process.readAllStandardOutput());

        //Split the QString every new line and save theve in a QStringList
        QStringList OutSplit = out.split("\n");

        //Parse the QStringList in btCellsParser
        btCellsParser cp(OutSplit);

        for(unsigned int i = 0; i<cp.count(); i++)
        {
           //writing in listwidget
        }

    }

    ScanDialogClose();
}

void BTConnect::on_ScanButton_clicked()
{
    //Scan for available nearby bluetooth devices
    ScanBTDevices();
}

上記のコードを使用すると、プロセスの開始時に qdialog scandialog が開き、データが qlistwidget にロードされると閉じますが、qdialog scandialog の内容は表示されません。show() を exec() に変更すると、内容は表示されますが、ダイアログが閉じられるまで QProcess は実行されません。

Qprocessの開始時にダイアログを開き、スキャンからのデータがqlistwidgetに読み込まれると閉じます。そしてscandialogの内容を表示させたい。2 つのラベルがあります。1 つは .GIF ファイルを含み、もう 1 つはスキャンを示すテキストを含みます。

どんな助けでも大歓迎です。

4

2 に答える 2

1

実行時にイベント ループに戻ることはなくshow(理由によりwaitForFinished)、実行時に処理コードに進むこともありません。exec

の代わりにwaitForFinished、シグナルに接続しfinishedてそこで処理し、それをキャンセルするシングル ショット タイマーを使用する必要があります。

void BTConnect::on_BTscanFinished()//new slot
{
   //Clear the list widget
    this->ui->listWidget->clear();

    //Read the command line output and store it in QString out
    QString out(BTscan_Process.readAllStandardOutput());

    //Split the QString every new line and save theve in a QStringList
    QStringList OutSplit = out.split("\n");

    //Parse the QStringList in btCellsParser
    btCellsParser cp(OutSplit);

    for(unsigned int i = 0; i<cp.count(); i++)
    {
       //writing in listwidget
    }
    ScanDialogClose();
}

void BTConnect::ScanBTDevices()
{
    ScanDialogShow();

    //Command to scan nearby bluetooth devices
    //"hcitool scan"
    QString cmd("hcitool scan");

    //start the process
    connect(BTscan_Process, SIGNAL(finished()), this, SLOT(on_BTscanFinished()));
    BTscan_Process.start(cmd);
    QTimer::singleShot(20000, scan, SLOT(close()));
}
于 2013-10-29T16:51:27.970 に答える
1

問題はQDialog::execQProcess::waitForFinished関数がイベント ループをブロックすることです。イベントループを決してブロックしないでください。したがって、物事をより非同期的に行う必要があります。

QProcessクラスは、のようなシグナルを使用して非同期に処理できますreadReadStandardOutput。また、スロットQDialogを使用して非同期に表示できます。open

例:

void ScanBTDevices() {
    // Open dialog when process is started
    connect(process, SIGNAL(started()), dialog, SLOT(open()));

    // Read standard output asynchronously
    connect(process, SIGNAL(readyReadStandardOutput()), SLOT(onReadyRead()));

    // Start process asynchronously (for example I use recursive ls)
    process->start("ls -R /");
}

void onReadyRead() {
    // Write to list widget
    dialog->appendText(QString(process->readAllStandardOutput()));
}

データは、プロセスによる生成中にダイアログに追加されます。また、QProcess::finishedシグナルを使用すると、ダイアログを閉じることができます。

于 2013-10-29T16:54:54.410 に答える