Results 1 to 3 of 3

Thread: How to connect qml slider into a button using cpp method ?

  1. #1
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post How to connect qml slider into a button using cpp method ?

    Hi all, i am trying to record,stop and play an audio file.What i am expecting is:when i click on the record button the slider should start moving showing the time duration and when i click on the stop button the slider should stop as required.

    This is my code and what i had tried so far:

    main.qml: import QtQuick 1.0
    import com.nokia.symbian 1.0
    import Qt 4.7

    Page {
    id: mainPage

    Button{
    id: rec
    x: 0
    y: 440
    width: 93
    height: 50
    text: "Record"
    anchors.bottomMargin: 210
    anchors.leftMargin: 0
    onClicked: {

    StringHelper.record();

    }

    }
    Button{
    id: b
    x: 98
    y: 440
    width: 86
    height: 50
    text: "Pause"
    anchors.bottomMargin: 210
    anchors.leftMargin: 0
    onClicked: {
    StringHelper.pause();

    }
    }


    Button{
    id: stp
    x: 188
    y: 440
    width: 82
    height: 50
    text: "Stop"
    anchors.bottomMargin: 210
    anchors.leftMargin: 30
    onClicked: {
    StringHelper.stop()
    }
    }


    Button{
    id: ply
    x: 272
    y: 440
    width: 89
    height: 50

    text: "Play"
    anchors.bottomMargin: 210
    anchors.leftMargin: 25
    onClicked: {
    StringHelper.play()
    }
    }

    Button{
    id: exit
    x: 295
    y: 22
    text: "Exit"
    MouseArea {id: mouseArea6;
    anchors.fill: parent
    onClicked: {Qt.quit();
    }
    }
    }

    TextEdit {
    id: text
    x: 82
    y: 22
    width: 142
    height: 30
    color: "#1339f5"
    text: qsTr("Voice Recorder")
    font.family: "Times New Roman"
    textFormat: TextEdit.RichText
    font.pixelSize: 30
    }

    Image {
    id: image
    x: 0
    y: 115
    width: 360
    height: 306
    source: "img.jpg"
    }

    Slider {
    id: slider
    x: 0
    y: 57
    width: 360
    height: 59
    orientation: 1
    stepSize: 1
    minimumValue: 0
    maximumValue: 30
    valueIndicatorVisible: true
    value: StringHelper.sliderValue

    }
    }

    stringhelper.h:

    #include <QObject>
    #include <QAudioCaptureSource>
    #include <QMediaRecorder>

    class StringHelper : public QObject
    {
    Q_OBJECT

    Q_PROPERTY (double sliderValue READ sliderValue WRITE setSliderValue NOTIFY sliderValueChanged)

    public:
    StringHelper();
    ~StringHelper();
    inline double sliderValue() const {return m_sliderValue;}

    public slots:
    void stop();
    void play();
    void record();
    QUrl generateAudioFilePath();
    void pause();
    void setSliderValue(double);

    signals:
    void sliderValueChanged(double newValue);


    private:
    QAudioCaptureSource *audioSource;
    QMediaRecorder *capture;
    bool active;
    double m_sliderValue;

    };

    #endif // STRINGHELPER_H

    stringhelper.cpp:

    #include "stringhelper.h
    #include<QSlider>
    #include<QMediaPlayer>
    #include<QDir>



    StringHelper::StringHelper()
    {
    audioSource = new QAudioCaptureSource();
    capture = new QMediaRecorder(audioSource);

    QAudioEncoderSettings es;
    es.setCodec("audio/mp3");
    es.setSampleRate(96000);
    capture->setEncodingSettings(es);
    capture->setOutputLocation(generateAudioFilePath());


    active = false;
    }

    StringHelper::~StringHelper()
    {
    delete audioSource;
    delete capture;
    }

    void StringHelper::record()
    {
    capture->record();
    active = true;
    setSliderValue(30); (from this code i was able to move the slider and recording is done but i am in need to move the slider by 1 as recording is done....?????????


    }

    void StringHelper::stop()
    {

    capture->stop();
    active = false;

    }
    void StringHelper:ause()
    {
    capture->pause();
    }


    void StringHelper:lay()
    {
    QMediaPlayer *player = new QMediaPlayer;
    QUrl url = QUrl::fromLocalFile("myvoice");
    player->setMedia(url);
    player->setVolume(100);
    player->play();


    }
    void StringHelper::setSliderValue(double value)
    {
    if (value == m_sliderValue)
    return;

    m_sliderValue = value;
    emit sliderValueChanged(m_sliderValue);
    }

    QUrl StringHelper::generateAudioFilePath()
    {
    QDir outputDir(QDir::rootPath());
    int lastImage = 0;
    int fileCount = 0;
    foreach(QString fileName, outputDir.entryList(QStringList() << "myvoice_*")) {
    int imgNumber = fileName.mid(5, fileName.size() - 9).toInt();
    lastImage = qMax(lastImage, imgNumber);
    if (outputDir.exists(fileName))
    fileCount += 1;
    }
    lastImage += fileCount;
    QUrl location(QDir::toNativeSeparators(outputDir.canoni calPath() + QString("/myvoice_%1").arg(lastImage + 1, 4, 10, QLatin1Char('0'))));
    return location;

    }

    main.cpp:

    #include <QtGui/QApplication>
    #include <QDeclarativeContext>
    #include "qmlapplicationviewer.h"
    #include "stringhelper.h"


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


    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::Screen OrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/Audio/main.qml"));


    QDeclarativeContext *context = viewer.rootContext();
    StringHelper myclass;
    context->setContextProperty("StringHelper", &myclass);

    viewer.showExpanded();
    return app.exec();


    }

    I have no idea how to link the slider to the button.Could you help me in finding the solution??

    Regards,
    Harish.
    Last edited by harish; 23rd February 2012 at 07:52.

  2. #2
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to connect qml slider into a button using cpp method ?

    to move a slider means that there is an end to the record.
    i mean, you have a minimum and a maximum value so if you want the slider to move you have to transform time into int value from 0 to 30 (your actual slider range), and the connect QMediaRecorder::durationChanged ( qint64 duration ) signal to a slot that will do the transformation and call StringHelper::setSliderValue(double value) slot.

  3. #3
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: How to connect qml slider into a button using cpp method ?

    Ya thank you Le i had done with this anyway thank you for your help

Similar Threads

  1. Replies: 2
    Last Post: 9th February 2012, 15:03
  2. Connect seek slider
    By klaviaturist in forum Qt Programming
    Replies: 2
    Last Post: 31st March 2011, 12:32
  3. Replies: 10
    Last Post: 3rd February 2011, 12:27
  4. error calling method using connect
    By jeffmetal in forum Newbie
    Replies: 4
    Last Post: 22nd April 2010, 19:09
  5. Replies: 13
    Last Post: 21st May 2009, 08:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.