如何在QMainWindow中使用QWinThumbnailToolBar

时间:2014-01-26 16:52:47

标签: c++ qt

我想在Qt 5.2中尝试QWinThumbnailToolBar,但它不起作用!(程序运行但没有缩略图!!!!)

//main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}


//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWinThumbnailToolButton>
#include <QWinThumbnailToolBar>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QWinThumbnailToolBar* thumbnailToolBar;
    QWinThumbnailToolButton *playToolButton;
    QWinThumbnailToolButton *forwardToolButton;
    QWinThumbnailToolButton *backwardToolButton;
};

#endif // MAINWINDOW_H


//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    thumbnailToolBar = new QWinThumbnailToolBar(this);
    thumbnailToolBar->setWindow(this->windowHandle());

    playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    playToolButton->setEnabled(false);
    playToolButton->setToolTip(tr("true"));
    playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));

    forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    forwardToolButton->setEnabled(true);
    forwardToolButton->setToolTip(tr("Fast forward"));
    forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));

    backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    backwardToolButton->setEnabled(true);
    backwardToolButton->setToolTip(tr("Rewind"));
    backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));

    thumbnailToolBar->addButton(backwardToolButton);
    thumbnailToolBar->addButton(playToolButton);
    thumbnailToolBar->addButton(forwardToolButton);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//pro file :

QT       += core gui winextras multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

我认为问题在于这两行:

 thumbnailToolBar = new QWinThumbnailToolBar(this);
 thumbnailToolBar->setWindow(this->windowHandle());

我也试过用QWidget代替QMainWindow ...... 我该如何解决?

2 个答案:

答案 0 :(得分:1)

您创建QWinThumbnailToolBar的代码是正确的,问题出在您创建它的位置。我认为在窗口构造函数中创建它是问题(也许是因为窗口句柄尚未准备好)。你可以做这样的事情:

// main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FrmMain w;
    w.show();
    w.createThmbBar();

    return a.exec();
}

其中createThumbBar()是一个公共函数,您可以在其中创建QWinThumbnailToolBar,如下所示:

// MainWindow.cpp

void MainWindow::createThmbBar()
{
    thumbnailToolBar = new QWinThumbnailToolBar(this);
    thumbnailToolBar->setWindow(this->windowHandle());

    playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    playToolButton->setEnabled(false);
    playToolButton->setToolTip(tr("true"));
    playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));

    forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    forwardToolButton->setEnabled(true);
    forwardToolButton->setToolTip(tr("Fast forward"));
    forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));

    backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
    backwardToolButton->setEnabled(true);
    backwardToolButton->setToolTip(tr("Rewind"));
    backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));

    thumbnailToolBar->addButton(backwardToolButton);
    thumbnailToolBar->addButton(playToolButton);
    thumbnailToolBar->addButton(forwardToolButton);
}

迟到的答案,但希望以后可以帮助任何人面对同样的问题。

答案 1 :(得分:0)

@Ramez提出的修复有效,但是由于Windows过多,我在应用程序关闭时崩溃。在析构函数中我需要做些特别的事情吗? QWinThumbnailToolBarPrivate::hasHandle()检查句柄时,QWindow已被删除。

Exception thrown: read access violation.
d was 0xFFFFFFFFFFFFFF7F.
QPlatformWindow *QWindow::handle() const
{
    Q_D(const QWindow);
    return d->platformWindow;
}

下面的堆栈跟踪:

    Qt5Guid.dll!QWindow::handle() Line 1929 C++
    Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::hasHandle() Line 460 C++
    Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::handle() Line 465    C++
    Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::nativeEventFilter(const QByteArray & __formal, void * message, long * result) Line 549   C++
    Qt5Cored.dll!QAbstractEventDispatcher::filterNativeEvent(const QByteArray & eventType, void * message, long * result) Line 484  C++
    [External Code] 
    Qt5Guid.dll!QWindowPrivate::destroy() Line 1914 C++
    Qt5Guid.dll!QWindow::destroy() Line 1864    C++
    Qt5Widgetsd.dll!QWidgetPrivate::deleteTLSysExtra() Line 1891    C++
    Qt5Widgetsd.dll!QWidget::destroy(bool destroyWindow, bool destroySubWindows) Line 12515 C++
    Qt5Widgetsd.dll!QApplication::~QApplication() Line 798  C++