Qt MainWindow没有更新

时间:2013-02-13 09:19:21

标签: multithreading qt signals-slots redraw

我正在使用Qt生成一个Window。另外我使用libnfc来访问nfc阅读器,到目前为止一直很好。 在我自己编写的nfc-class中,我生成一个新线程,这个线程正在轮询读取器上的新标签。如果有新标签,则线程将为MainWindow启动信号事件。 在主窗口中,我只有一个QWebView,它将显示不同状态的不同网站(开始后,新标签,标签已删除),只是真正的基本内容。

我现在的问题是:主窗口(或QWebView)没有更新。如果我切换到另一个程序并返回我的应用程序,该窗口将更新。我已经在谷歌搜索并尝试不同的东西,但没有任何帮助。

这里是线程代码:

class NFC_Thread : public QThread
{
    Q_OBJECT
public:
    NFC_Thread(NFC_Reader * Reader);
    void run();

signals:
    void NewTarget(nfc_target Target);
    void TargetRemoved(nfc_target Target);

private:
    int mError;
    bool mStopPolling;
};

void NFC_Thread::run()
{
    mError = 0;
    mStopPolling = false;
    while(!mStopPolling)
    {
        nfc_target Target;
        mError = nfc_initiator_poll_target(mReader->GetDevice(), nmModulations, szModulations, mPollNr, mPollPeriod, &Target);
        if(mError > 0)
        {
            cout << "NFC: found target" << endl;
        }
#warning Bug in driver: Timeout generate a NFC_EIO Error, 'https://code.google.com/p/libnfc/issues/detail?id=224'
        else if(mError > 0)
        {
            cout << "NFC: Error" << endl;
            mStopPolling = true;
        }
        else
        {
            cout << "NFC: no target found" << endl;
        }
    }
}

MainWindow代码:

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
     void SetNewTarget(nfc_target Target);
     void doTargetRemoved(nfc_target Target);

private:
    bool event(QEvent *event);
    void resizeEvent(QResizeEvent *);
    void adjust();

    Ui::MainWindow *ui;
    QWebView * mWebView;
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mWebView = new QWebView(this);
    mWebView->load(QUrl("http://www.pbuchegger.at/"));
    mWebView->show();
}

void MainWindow::SetNewTarget(nfc_target Target)
{
    QString str = "NEW TARGET: \n";
    {
        char * s;
        str_nfc_target(&s, Target, false);
        str += s;
        delete s;
    }
    //cout << "NFC: Target: " << str << endl;
    mWebView->load(QUrl("http://www.google.at"));
    update();
    repaint();
    mWebView->update();
    qApp->processEvents();
    /*QMessageBox msgBox;
    msgBox.setText(str);
    msgBox.exec();*/
}

void MainWindow::doTargetRemoved(nfc_target Target)
{
    QString str = "TARGET REMOVED: \n";
    {
        char * s;
        str_nfc_target(&s, Target, false);
        str += s;
        delete s;
    }
    //cout << "NFC: Target: " << str << endl;
    mWebView->load(QUrl("http://www.cde.at"));
    update();
    repaint();
    mWebView->update();
    qApp->processEvents();
    /*QMessageBox msgBox;
    msgBox.setText(str);
    msgBox.exec();*/
}

bool MainWindow::event(QEvent *event)
{
    if(event->type() == QEvent::Resize)
    {
        adjust();
        return true;
    }
    return false;
}

void MainWindow::resizeEvent(QResizeEvent *)
{
    adjust();
}

void MainWindow::adjust()
{
    mWebView->setGeometry(0, 0, ui->centralWidget->geometry().width(), ui->centralWidget->geometry().height());
}

主要代码:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qRegisterMetaType<nfc_target>("nfc_target");

    MainWindow w;
    w.setWindowState(Qt::WindowMaximized);

    NFC_Reader Reader;
    nfc_device_string devs;
    size_t nr;
    QString str = "";

    Reader.GetDevices(devs, nr);
    if(nr > 0)
    {
        if(!Reader.InitReader(NULL))
        {
            str += "Error on init!";
        }
        else
        {
            Reader.Start_Polling();
            str += "Started Polling!";
        }
    }
    else
    {
        str += "No Device found!";
    }
    w.SetText(str);

    SignalHelper Helper;

    QObject::connect(Reader.GetThread(), SIGNAL(NewTarget(nfc_target)), &Helper, SLOT(doNewTarget(nfc_target)));
    QObject::connect(Reader.GetThread(), SIGNAL(TargetRemoved(nfc_target)), &Helper, SLOT(doTargetRemoved(nfc_target)));
    QObject::connect(&Helper, SIGNAL(NewTarget(nfc_target)), &w, SLOT(SetNewTarget(nfc_target)));
    QObject::connect(&Helper, SIGNAL(TargetRemoved(nfc_target)), &w, SLOT(doTargetRemoved(nfc_target)));

    w.show();
    int ret = a.exec();
    Reader.Abort_Polling();
    return ret;
}

正如你所看到的,我有一个&#34; Helper&#34; class,这个类只是在一个槽中获取信号并再次启动一个信号,该信号将转发到主窗口。如果我想将信号直接转发到主窗口,则不会发生任何事情(如信号未被触发),但我正在使用Qt-About框检查它,并且框出现了。

助手类:

class SignalHelper : public QObject
{
    Q_OBJECT
public slots:
    void doNewTarget(nfc_target Target);
    void doTargetRemoved(nfc_target Target);
signals:
    void NewTarget(nfc_target Target);
    void TargetRemoved(nfc_target Target);
};

void SignalHelper::doNewTarget(nfc_target Target)
{
    emit NewTarget(Target);
}

void SignalHelper::doTargetRemoved(nfc_target Target)
{
    emit TargetRemoved(Target);
}

没有编译器错误或链接器错误。这段代码只展示了重要的东西,所有不重要的东西都被删除了。仅供您参考项目文件:

QT += core gui testlib
QT += webkit

greaterThan(QT_MAJOR_VERSION, 4) {
 QT +=  widgets
}

TARGET = NFC_GUI
TEMPLATE = app

SOURCES += main.cpp \
 mainwindow.cpp \
 nfc_thread.cpp \
 nfc_reader.cpp \
 signal_helper.cpp

HEADERS += mainwindow.h nfc_thread.h nfc_reader.h signal_helper.h

FORMS += mainwindow.ui

LIBS += -lnfc

2 个答案:

答案 0 :(得分:2)

让我的评论回答:

你的功能

bool MainWindow::event(QEvent *event)
{
    if(event->type() == QEvent::Resize)
    {
        adjust();
        return true;
    }
    return false;
}
除了QMainWindow之外,

会处理QEvent::Resize中处理的任何事件。您需要为不感兴趣的事件调用默认行为:

bool MainWindow::event(QEvent *event)
{
    if(event->type() == QEvent::Resize)
    {
        adjust();
        return true;
    }
    // call the parent implementation
    return QMainWindow::event(event);
}

请注意,您也可以简单地实施QWidget::resizeEvent

void MainWindow::resizeEvent(QResizeEvent *event)
{
    adjust();

    QMainWindow::resizeEvent(event);
}

答案 1 :(得分:0)

如果要从不同线程之间的信号调用插槽,则需要使用Qt :: QueuedConnection作为连接类型进行连接调用。