Qt World Summit: Register Today!

Twitter search by JSON respond

  • Hello every body
    I'm learning how to do a simple twitter search using JSON as respond,every thing works just fine, except that the text of the tweet doesn't appear.

    Here's the code

    @#ifndef CONNECTOR_H
    #define CONNECTOR_H
    #include <QNetworkAccessManager>
    #include <QObject>

    class connector : public QObject
    explicit connector(QObject *parent = 0);
    Q_INVOKABLE void search(QString query);
    void searchSignal(QString jsonSearch);
    public slots:
    void searchResultGot();
    QNetworkAccessManager *nm;

    #endif // CONNECTOR_H

    @#include "connector.h"
    connector::connector(QObject *parent) :
    nm = new QNetworkAccessManager(this);

    void connector::search(QString query)
    QString request = "http://search.twitter.com/search.json?from=";
    QNetworkReply *rep = nm->get(QNetworkRequest(request));

    void connector::searchResultGot()
    QNetworkReply *rep = qobject_cast<QNetworkReply *>(sender());
    if(rep->error() == QNetworkReply::NoError)
    QByteArray bytes = rep->readAll();
    emit searchSignal(QString(bytes));
    else {
    qDebug()<<"Baso!,Erro while downloading Data"<<rep->errorString();

    int main (int argc, char**argv)
    QApplication app(argc,argv);
    QDeclarativeView view;
    return app.exec();

    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    id: wrapper
    property alias text: input.text
    width: 300; height: 40;
    y: 10; radius: 25; border {color: 'black'; width: 1} smooth: true
    TextInput {
    focus: true
    id: input
    anchors.centerIn: parent
    width: parent.width - 50
    Text {
    visible: input.text == ''; color: 'gray'
    text: 'enter text'; anchors.verticalCenter: parent.verticalCenter
    x: 25

    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import Module 1.0
    Item {
    id: root
    width: 360; height: 640

        onSearchSignal: {
            var jsonObj = JSON.parse(jsonSearch)
            var tweetsArray = jsonObj.results
            var array = Array
            var array2 = Array
            for(var i = 0; i < tweetsArray.length; i++){
    LineEdit {
        focus: true
        id: searchInput
        anchors.top: parent.top; anchors.left: parent.left; anchors.leftMargin: 15
        width: 240
    Rectangle {
        id: searchButton
        width: 80; height: 40; color: 'lightBlue'; radius: 15
        anchors {top: parent.top; right: parent.right; rightMargin: 15}
        MouseArea {
            anchors.fill: parent
            onClicked: {
                if(searchInput.text != ''){
                    tweetsModel.clear() // to handle a search after another one
            anchors.centerIn: parent; text: 'search'
    ListModel {
        id: tweetsModel
        width: parent.width; anchors.top: searchButton.bottom; anchors.topMargin: 25; anchors.bottom: parent.bottom
        model: tweetsModel; clip: true; cacheBuffer: 1200
        delegate: Rectangle{
            id: thum
            color: "lightgray"
            border.color: "black"; border.width: 1
            radius: 20
            width: parent.width-1
            height: 80
            Image {
                id: profilePic
                source: profile_image_url
                anchors{/*left: parent.left;*/ verticalCenter: parent.verticalCenter; leftMargin: 10; horizontalCenter: textname.horizontalCenter}
                smooth: true
            Text {
                id: textname
                anchors{top: parent.top; bottom: profilePic.top; left: parent.left; leftMargin: 15}
                text: qsTr(from_user_name)
            Text {
                id: texttweet
                anchors{left: profilePic.right; verticalCenter: profilePic.verticalCenter; leftMargin: 15}
                text: qsTr(text)
                color: "blue"


  • hi Baso,

    I think that your query doesn't work. I tryed that but always had 0 results, so no output.

    Instead of

    bq. QString request = "http://search.twitter.com/search.json?from=";

    try with

    bq. QString request = "http://search.twitter.com/search.json?q=";


  • marcoB
    Thanks for reply, but the app is not to search for persons on twitter, it's about getting the tweet of a specific person.
    What I want to do is to write the twitter name of someone and click search, and the app displays the last 15 tweets of that person, So I think the query is write because I get the profile pic of the name on the search, but not the tweets that's it.
    any tip about how to get that tweets from the JSON.

  • I tryed your source and the workflow does the job. Whats wrong is the twitter query you are using. With that jsonObj.results will be always empty. I never used twitter APIs, but you can find docs about getting user tweets "here":https://dev.twitter.com/docs/api/1/get/statuses/user_timeline.

    I think you shoud use something like:

    bq. QString request = "http://api.twitter.com/1/statuses/user_timeline.json?count=5&screen_name=";

    in your search() function, but the json structure is different, so no jsonObj.results
    To better undestand how is structured your query result and manage your code, try to use your browser and point it to

    bq. http://api.twitter.com/1/statuses/user_timeline.xml?count=5&screen_name=<USER>

    here I used xml instead of json because for example in my browser, the xml format is formatted and more readable.

    hope this helps

  • I'v used your query and made the required changes, but the same here I get the profile pic and the user name, but not the text of the tweet
    But this time I got error here's it

    @Error: qsTr(): first argument (text) must be a string@

    I don't know why it can't see the text as a string, may be it's about coding or something!, I don't know.
    And about the XML , I know XML much easier, but I wanna do it in JSON.
    Thanks :)

  • Your first version does work. Problem is main.qml:70: text: qsTr(text)
    You are trying to set Text element property text to text which loops back to same property.
    change it to: tweetsModel.get(index).text

    Also I see no reason to use qsTr() for strings which get from network request.

  • Tomma, Thank you, this really fixed the problem
    Thank you

Log in to reply