ページ

2013年11月27日水曜日

サインカーブ


.pro ファイルに
CONFIG += qwt
を追記

ヘッダー
plot.h
#ifndef PLOT_H
#define PLOT_H

#include <QMainWindow>
#include <qwt_plot.h>
#include <qwt_legend.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>

namespace Ui {
class Plot;
}

class Plot : public QwtPlot
{
    Q_OBJECT
    
public:
    explicit Plot(QWidget *parent = 0);
    ~Plot();
    
private:
    Ui::Plot *ui;
    class QwtPlotCurve *curve;
    void plotCurve();
};

#endif // PLOT_H

main.cpp
#include "plot.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qDebug("%s", "main.cpp");

    Plot w;
    w.show();
    
    return a.exec();
}


#include "plot.h"
#include "ui_plot.h"


Plot::Plot(QWidget *parent) :
    QwtPlot(parent),
    ui(new Ui::Plot)
{
    //ui->setupUi(this);

    setTitle( "Plot Demo" );//グラフのタイトル
    insertLegend( new QwtLegend() );// 凡例を表示

    // y軸
    setAxisTitle(QwtPlot::yLeft, "y abc");//タイトル
    //setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );//目盛

    // x軸
    setAxisTitle(QwtPlot::xBottom, "x abc");

    // グリッドの設定
    QwtPlotGrid *grid = new QwtPlotGrid();
    grid->enableXMin(true);
    grid->attach( this );


    curve = new QwtPlotCurve();
    curve->setTitle("sine wave");
    curve->setRenderHint(QwtPlotItem::RenderAntialiased);
    curve->setPen(QPen(Qt::red));
    curve->attach(this);

    // 曲線の描画
    plotCurve();

    curve->attach( this );

    resize( 600, 400 );

    //setAxisScale( QwtPlot::xBottom, 0.5, 2.0 );//目盛
}

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

void Plot::plotCurve()
{
    const int kArraySize = 1000;

    double x[kArraySize] = {}; // x
    double y[kArraySize] = {}; // y

    for (int i = 0; i < kArraySize; ++i) {
        x[i] = i/(kArraySize-1.0);
        y[i] = sin(2.0*M_PI*x[i]);
    }

    curve->setSamples(x, y, kArraySize);
}








2013年11月23日土曜日

Qwtのインストール・メモ

Qt のグラフライブラリ インストールに苦労したのでメモ

使用したもの
windows 8 64bit
Qt 5.1.1 for Windows 32-bit (MinGW 4.8, OpenGL, 666 MB)
qwt-6.1.0.zip

1.Qt をインストールしてシステム環境変数を登録
C:\Qt\Qt5.1.1\5.1.1\mingw48_32\bin
C:\Qt\Qt5.1.1\Tools\mingw48_32\bin

2.qwt-6.1.0.zipを解凍する
コマンドプロンプトを開く。
qwt-6.1.0.zipを解凍したフォルダ(C:\temp\qwt-6.1.0)で次のコマンドを打つ。
qmake qwt.pro
mingw32-make
mingw32-make install

成功すると
C:\Qwt-6.1.0 が出来上がる。

3.システム環境変数を登録
C:\Qwt-6.1.0\lib

ユーザー環境変数を登録する。
変数:QT_PLUGIN_PATH  値:C:\Qwt-6.1.0\plugins;
変数:QMAKEFEATURES  値:C:\Qwt-6.1.0\features;


環境変数を反映させるために再起動させる。
これで完了。

参考にしたサイト:HowTo: Installation of Qt 5.0.1 and Qwt 6.1.0 rc3 (Win7 64bit)


以下苦労した点 
mingw32-make の時エラーが出た。

collect2.exe: error: ld returned 1 exit status
Makefile.Release:320: recipe for target '../lib/qwt.dll' fa
mingw32-make[2]: *** [../lib/qwt.dll] Error 1
mingw32-make[2]: Leaving directory 'D:/qwt-6.1.0/src'
Makefile:38: recipe for target 'release-all' failed
mingw32-make[1]: *** [release-all] Error 2
mingw32-make[1]: Leaving directory 'D:/qwt-6.1.0/src'
makefile:41: recipe for target 'sub-src-make_first-ordered'
mingw32-make: *** [sub-src-make_first-ordered] Error 2

たぶん以前に利用していたMinGW を使っていたからだと思う

備考:
>qmake -set QMAKEFEATURES C:\Qwt-6.0.1\features と解説している所もあった。

> qmake -unset QMAKEFEATURES
> set QMAKEFEATURES=C:\Qwt-6.1.0\features
> qmake  
>



>mingw32-make install の時に
mingw32-make: *** No rule to make target 'inatall'.  Stop. のエラーが出た時は再起動

qwt widget は使えない。プラグインが読み込めていないみたいです。

QCustomPlot というライブラリもあるようです。

2013年11月14日木曜日

サーバープログラム:QTcpSocket

Qt GUI アプリケーション でプロジェクトを作成する。

とりあえず接続の確認とボタンを押してクライアント側に文字を送ってみる。

プロジェクト名:tcpServer

tcpServer.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-11-11T20:59:55
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = tcpServer
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpServer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

protected slots:
    void connection();
    void deleteLater();
    void read();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    QTcpServer *tcpServer;
    QTcpSocket *socket;
};

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

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

    return a.exec();
}
mainwoidow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTcpSocket>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qDebug("%s", "処理スタート");
    
    ui->setupUi(this);
    
    //QTcpServer *tcpServer;
    int port = 1024;
    
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any, port);
    
    // クライアントと接続した時の処理、SLOT関数を作成する。
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(connection()));
}

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

void MainWindow::connection()
{
    qDebug("%s", "接続完了");
    
    // まず最初に、接続してきたクライアントのソケットを取得する。
    socket = tcpServer->nextPendingConnection();
    
    // 切断したときの処理としてSLOT関数を設定する。
    connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    // クライアントからの送信を受信する
    connect(socket, SIGNAL(readyRead()), this, SLOT(read()));
    
    // 一応、クライアントのアドレスを取得する。
    QString ippaddr = socket->localAddress().toString();
    int ippaddr2 = socket->localPort();
    
    qDebug("%s", qPrintable( ippaddr ));    //ip アドレス表示
    qDebug("%d", ippaddr2 );                //ポート番号表示
    
    //クライアントに文字を送信するためのストリームを定義
    QDataStream out(socket);
    out.setVersion(QDataStream::Qt_4_1);
    
    // データを送信。
    QString string = "Send";
    out << quint16(string.size()) <<  string << quint16(0xFFFF);
    
}

/*
 *概要:クライアントが切断時したの処理
*/
void MainWindow::deleteLater()
{
    qDebug("%s", "deleteLater");
}
/*
 *概要:クライアントからの送信を受信
 */
void MainWindow::read()
{
    qDebug("%s", "on_readyRead");
    
    char buffer[128];
    
    socket->read(buffer, socket->bytesAvailable());
    std::string sString(buffer);
    QString qString(sString.c_str());
    
    ui->textEdit->setText(qString);
    qDebug("%s", qPrintable(qString));
    
    qDebug("%s", "on_readyRead end");
}
/*
 *概要:ボタンがクリックされた時にクライアントに送信する
*/
void MainWindow::on_pushButton_clicked()
{
    qDebug("%s", "ボタンがクリックされた");
    
    QDataStream out2(socket);
    out2.setVersion(QDataStream::Qt_4_1);
    
    QString string = "Buton Send";
    out2 << quint16(string.size()) <<  string << quint16(0xFFFF);
}

UI のデザイン
ボタンにはスロットのclicked()を設定している。

動作確認
クライアントソフトはベクターから『TCPテスター』をダウンロードしてインストールする。
参考:クライアントソフトはいろいろありますが『TCPテスター』がいい感じです。

1.作成したサーバプログラムを実行させる。

2.『TCPテスター』で、動作モードでクライアントを選択して、接続ボタンを押す。
ipアドレスはそれぞれ設定して下さい。ポート番号はプログラムで指定した番号。
接続したときに、サーバから文字sendを送っているのが確認できる。

3.クライアントからサーバに文字を送る
『TCPテスター』のASCII文字列に文字を入力して、送信ボタンを押す。

一応、クライアントから送られた文字を受信できているのが確認できる。

3.サーバのプッシュボタンを押すと、『TCPテスター』で、 Buton Send と送られるのが確認できる。

サーバ側のプログラムで、送信の時の文字の先頭や、受信時の文字の最後に変な空白か何かが在るが、とりあえずは、送受信が確認できた。

備考:
このプログラムを書いたのが、Qtを使って1週間くらいなので、まだわからないことが多い。







2013年11月10日日曜日

『アプリケーション出力』画面に文字を表示する。

#include <QDebug>

qDebug("qDebug, printf style, %i\n", 1);
qDebug() << "qDebug cout style, " << 1;


BloggerでSyntaxHighlighterを使う。

ガジェットからSyntaxHighlighterを使う。

<script type='text/javascript'>
function loadCss(url) {
 var headElem  = document.getElementsByTagName('head')[0]
 var linkElem =document.createElement("link")
 linkElem.rel = "Stylesheet"
 linkElem.type = "text/css"
 linkElem.href = url
 headElem.appendChild(linkElem)
}
loadCss("http://alexgorbatchev.com/pub/sh/current/styles/shCore.css");
loadCss("http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css");
</script>

<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type='text/javascript' ></script>

<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js"  ></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" ></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js" ></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js" ></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js" ></script>

<script language='javascript' type="text/javascript">
<!--
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
// -->
</script>