Qt MainWindow 未更新 [英] Qt MainWindow is not updating

查看:74
本文介绍了Qt MainWindow 未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt 生成一个窗口.此外,我使用 libnfc 来访问 nfc 阅读器,到目前为止一切顺利.在我自己编写的 nfc 类中,我生成了一个新线程,该线程正在轮询阅读器上的新标签.如果有新标签,线程将为 MainWindow 启动一个信号事件.在主窗口中,我只有一个 QWebView,它将显示不同状态的不同网站(启动后,新标签,标签删除),只是非常基本的东西.

I am using Qt to generate a Window. Additionally I use libnfc to get access to a nfc reader, so far so good. In my self written nfc-class i generate a new thread, this thread is polling for new tags on the reader. If there is a new tag, the thread will start a signal event for the MainWindow. In the main window I have just a QWebView which will show different websites on different states (after start, new tag, tag removed), just realy basic stuff.

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

My problem is now: that the main window (or the QWebView) is not updating. If i switch to another programm and go back to my app, the window will be updated. I was already searching with google and trying different stuff but nothing helps.

这里是线程代码:

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;
        }
    }
}

主窗口代码:

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;
}

正如你所看到的,我有一个Helper"类,这个类只是在一个插槽中获取信号并再次启动一个将转发到主窗口的信号.如果我想将信号直接转发到主窗口,什么也没有发生(比如信号没有被触发),但是我正在使用 Qt-About 框检查它,并且该框出现了.

As u can see, I have a "Helper" class, this class is just getting the signal in a slot and starting again a signal which will be forward to the mainwindow. If i want to forward the signal directly to the mainwindow, nothing is happening (like the signal is not fired), but i was checking it with the Qt-About box, and the box is showing up.

助手类:

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);
}

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

no compiler errors or linker errors. this code shows just the important stuff, all the unimportant stuff is removed. just for your information the project file:

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

推荐答案

将我的评论作为答案:

你的功能

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

处理在 QMainWindow 中处理的任何事件,除了 QEvent::Resize.您需要为您不感兴趣的事件调用默认行为:

eats any event which is handled in QMainWindow except for QEvent::Resize. You need to call the default behaviour for events you are not interested in:

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

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

Note you can also just simply implement QWidget::resizeEvent:

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

    QMainWindow::resizeEvent(event);
}

这篇关于Qt MainWindow 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆