3

A experienced the following bug in Qt 4.8.5, under Ubuntu 13.04 (and I'm nem to Qt) I have have an application with the following structure:

Mainwondow
-CentralWidget
--VerticalLayout
---TabWidget
---QLabel (created with code, and added to the layout)
---StatusBar

In fullscreen mode I hide the TabWidget, and the Statusbar, then the QLabel stops refreshing. (i have a thread to do the refresh) The strange thing is, when i restore the TabWidget or the StatusBar it works fine. It also works good, if i add a 1x1 pixel label to the VerticalLayout.

The slot responsible for the gui change;

void Mainview::onToggleFullScreen()
{
    if (this->isFullScreen())
    {
        this->showNormal();
        this->statusbar->show();
        this->tabWidget->show();
    }
    else
    {
        this->showFullScreen();
        this->statusbar->hide();
        this->tabWidget->hide();
    }
}

But the thing I cant understand if I put a QLabel near the image, it works, and if I add this single line to the MainWindow constructor, it stops refreshing:

label_10->hide(); //this is the label

Any idea what is the problem?

(Thanks in advance)

4

1 回答 1

0

您可能以某种错误的方式进行操作,但您没有显示代码,那么我们怎么知道呢?

下面是一个安全的SSCCE,说明如何做到这一点。在 Qt 4.8 和 5.1 下工作。

Nitpick:状态栏不应该是centralWidget()! QMainWindow为您提供了一个statusBar()

在线程之间传递图像的唯一安全方法是 via QImage。除了在 GUI 线程中,您不能在任何地方使用。QPixmap故事到此结束。

在下面的示例中,所有重要的事情都发生在幕后。生活DrawThing QObject在另一个线程中。这QThread是该方法的默认实现run()旋转消息循环。这就是计时器可以触发的原因,您需要一个旋转的消息循环。

每次生成新图像时,都会通过隐式发布消息将其传输到 GUI 线程MainWindow。该消息由 Qt 事件循环代码接收并重新合成为插槽调用。DrawThing 这是因为连接(和实例)的两端MainWindow位于不同的线程中。

Qt 的“代码更少,创造更多”设计方法的美妙之处:) 你越是利用 Qt 为你做的事情,你就越不需要担心样板文件。

截屏

//main.cpp
#include <QMainWindow>
#include <QVBoxLayout>
#include <QStatusBar>
#include <QLabel>
#include <QThread>
#include <QPainter>
#include <QImage>
#include <QApplication>
#include <QBasicTimer>
#include <QPushButton>

class DrawThing : public QObject {
    Q_OBJECT
    int m_ctr;
    QBasicTimer t;
    void timerEvent(QTimerEvent * ev) {
        if (ev->timerId() != t.timerId()) return;
        QImage img(128, 128, QImage::Format_RGB32);
        QPainter p(&img);
        p.translate(img.size().width()/2, img.size().height()/2);
        p.scale(img.size().width()/2, img.size().height()/2);
        p.eraseRect(-1, -1, 2, 2);
        p.setBrush(Qt::NoBrush);
        p.setPen(QPen(Qt::black, 0.05));
        p.drawEllipse(QPointF(), 0.9, 0.9);
        p.rotate(m_ctr*360/12);
        p.setPen(QPen(Qt::red, 0.1));
        p.drawLine(0, 0, 0, 1);
        m_ctr = (m_ctr + 1) % 12;
        emit newImage(img);
    }
public:
    explicit DrawThing(QObject *parent = 0) : QObject(parent), m_ctr(0) { t.start(1000, this); }
    Q_SIGNAL void newImage(const QImage &);
};

class MainWindow : public QMainWindow {
    Q_OBJECT
    QLabel *m_label;
public:
    explicit MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0) : QMainWindow(parent, flags) {
        QWidget * cw = new QWidget;
        QTabWidget * tw = new QTabWidget();
        QVBoxLayout * l = new QVBoxLayout(cw);
        l->addWidget(tw);
        l->addWidget(m_label = new QLabel("Label"));
        setCentralWidget(cw);
        QPushButton * pb = new QPushButton("Toggle Status Bar");
        tw->addTab(pb, "Tab 1");
        connect(pb, SIGNAL(clicked()), SLOT(toggleStatusBar()));
        statusBar()->showMessage("The Status Bar");
    }
    Q_SLOT void setImage(const QImage & img) {
        m_label->setPixmap(QPixmap::fromImage(img));
    }
    Q_SLOT void toggleStatusBar() {
        statusBar()->setHidden(!statusBar()->isHidden());
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QThread t;
    DrawThing thing;
    MainWindow w;
    thing.moveToThread(&t);
    t.start();
    w.connect(&thing, SIGNAL(newImage(QImage)), SLOT(setImage(QImage)));
    w.show();
    t.connect(&a, SIGNAL(aboutToQuit()), SLOT(quit()));
    int rc = a.exec();
    t.wait();
    return rc;
}

#include "main.moc"
于 2013-08-26T18:42:05.883 回答