天天看點

LINUX用QT進行圖像顯示

由于項目需要, 需要在LINUX上将兩個攝像頭的資料同時顯示在一個最大化窗體上,實作監控。  以前都比較習慣在WINDOWS平台上用MFC或JAVA寫界面, 轉到LINUX上,隻好求取于QT大神了。 

下面是主函數源檔案

#include <QApplication>
#include "window.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
}
           

窗體源檔案

/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtGui>
#include "glwidget.h"
#include "widget.h"
#include "window.h"

//! [0]
Window::Window()
    : QWidget()
{
    Widget *native = new Widget(&helper1, this);
    //Widget *cam2   = new Widget(&helper, this);

    GLWidget *openGL = new GLWidget(&helper2, this);

    QLabel *nativeLabel = new QLabel(tr("IPCAM-1"));
    nativeLabel->setAlignment(Qt::AlignHCenter);
    QLabel *openGLLabel = new QLabel(tr("IPCAM-2"));
    openGLLabel->setAlignment(Qt::AlignHCenter);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(native, 1, 0);
    layout->addWidget(openGL,   1, 1);
    layout->addWidget(nativeLabel, 0, 0);
    layout->addWidget(openGLLabel, 0, 1);
    setLayout(layout);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), native, SLOT(animate()));
    connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
    timer->start(50);

    setWindowTitle(tr("2D Painting on IPCAM-1 and IPCAM-2 Widgets"));
}
//! [0]
           

以上代碼也挺容易看懂的,跟JAVA的窗體有點相像個人覺得, 聲明了兩個Widget類, 對兩個顯示部分獨立顯示, 後面用一個架構管理容器将它們包含進去, 後面加入一個定時器,50ms去重新整理一張圖檔, 這裡有用到QT的信号與槽, 這裡是跟MFC有些不一樣的地方, MFC用的是消息映射機制, 微軟弄的一套機制, 而JAVA用的是事件響應,其實都是大同小異的, 懂其中一個,另外的都是可以融會貫通的。 

後面就不一一貼出來了,大體思路就是定時去重新整理圖檔區,我用的是QPainter的drawImage方法來進行繪圖。 有需要可以下我的工程來進行修改, 當時也隻是一個DEMO例子而已。 工程連結: 點選打開連結