Results 1 to 19 of 19

Thread: Type conversion on container templates

  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Type conversion on container templates

    Actually i did alot changing on code and googling, if it's make you feel better, i suffering myself to don't ask a question!
    But i have to do :|


    I have c++ class with a this function
    Qt Code:
    1. std::string addUri(std::vector<std::string> uris, std::map<std::string, std::string> options);
    To copy to clipboard, switch view to plain text mode 
    All i want to do is make a qt wrap function for it
    Like:
    Qt Code:
    1. QString addUri(QVector<QString> uris, QMap<QString, QString> options);
    To copy to clipboard, switch view to plain text mode 

    The wrap one isn't right
    I will apperciate to help me in this conversion
    tahnks
    Last edited by Alir3z4; 27th January 2012 at 23:45. Reason: updated contents
    ...یه مرد هیچوقت زمین نمیخوره

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Type conversion on container templates

    What's "not right" about it?

  3. #3
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Type conversion on container templates

    passing std::vector<std::string> uris, std::map<std::string, std::string> options by value is a lot wwrse than passing most Qt types by value. You should be passing stl containers by const ref, not value.

    If you conversion from stl to Qt is not correct, then you should show us your attempt at that code.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Type conversion on container templates

    Here you can see the source code, it's on the git
    yoDownet/yoDownloaders/aria2c.h
    aria2c.h is a c++ source, which i wan't to make a qt function wraper for it.
    there is a qt/class called Downloader which my program access to aria2c.h throght it.
    i don't know how to implement such function to handle type conversion.
    Qt Code:
    1. QString addUri(QVector<QString> uris, QMap<QString, QString> options);
    To copy to clipboard, switch view to plain text mode 
    i hope i explained my point as well

    ##update:
    i think i didn't push the local git repo to remote, that's why the Downloader class isn't available on the git :|
    Last edited by Alir3z4; 28th January 2012 at 14:08. Reason: updated contents
    ...یه مرد هیچوقت زمین نمیخوره

  5. #5
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Type conversion on container templates

    There are several steps:
    How do you convert QString to std::string?
    How do you convert QVector to std::vector?
    How do you convert QMap<QString, QString> to std::map<std::string, std::string>?

    QString qs("abc");
    std::string x(qs.toStdString());

    QVector<QString> = qsv;
    qsv.push_back(qs);

    std::vector<std::string> sv;
    sv.push_back(qs.toStdString());

    or

    sv.resize(qsv.size());
    sv[some_index] = qsv[some_index].toStdString();

    etc.

    if you feel adventurous, you can do it in very few lines with std::transform and a suitable functor
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. The following user says thank you to amleto for this useful post:

    Alir3z4 (29th January 2012)

  7. #6
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Exclamation Re: Type conversion on container templates

    The actual thing i wanna do is making conversion at the template argument, not getting them one by one!
    like:
    Qt Code:
    1. const QString addUri(const QVector< const std::string(QString::toStdString()) > uris);
    To copy to clipboard, switch view to plain text mode 
    huh? what are you talking about dah?
    but all i've got every time is delicious error :|
    Last edited by Alir3z4; 29th January 2012 at 17:59. Reason: updated contents
    ...یه مرد هیچوقت زمین نمیخوره

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    I'm sorry, what exactly is the problem? You have problems with function signature or with function body? What is the error you are getting?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Exclamation Re: Type conversion on container templates

    Quote Originally Posted by wysota View Post
    I'm sorry, what exactly is the problem? You have problems with function signature or with function body? What is the error you are getting?
    i didn't even implement the function body
    this is the error!

    Qt Code:
    1. make: Entering directory `/home/alireza/dev/qt/yoDownet-build-desktop-Qt_in_PATH_Debug'
    2. g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++ -I../yoDownet -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtSql -I/usr/include -I. -I. -I../yoDownet -I. -o downloader.o ../yoDownet/downloader.cpp
    3. In file included from ../yoDownet/downloader.cpp:1:0:
    4. ../yoDownet/downloader.h:17:77: error: template argument 1 is invalid
    5. make: Leaving directory `/home/alireza/dev/qt/yoDownet-build-desktop-Qt_in_PATH_Debug'
    6. make: *** [downloader.o] Error 1
    7. 22:36:00: The process "/usr/bin/make" exited with code 2.
    8. Error while building project yoDownet (target: Desktop)
    9. When executing build step 'Make'
    To copy to clipboard, switch view to plain text mode 

    Don't tell me give us the source code, coz the whole header file goes to 5 lines :|
    Last edited by Alir3z4; 29th January 2012 at 18:33. Reason: updated contents
    ...یه مرد هیچوقت زمین نمیخوره

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    And the exact line triggering the error (downloader.h:17 and :77)?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Type conversion on container templates

    Quote Originally Posted by wysota View Post
    And the exact line triggering the error (downloader.h:17 and :77)?
    == give me the source code :|
    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QVector>
    7. #include <QMap>
    8. #include "yoDownloaders/aria2c.h"
    9.  
    10. class Downloader : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit Downloader(QObject *parent = 0);
    15.  
    16. // Operations :|
    17. const QString addUri(const QVector<QString> &uris);
    18. QString addUri(QVector<QString> &uris, QMap<QString, QString> &options);
    19.  
    20. private:
    21. Aria2c aria2;
    22.  
    23.  
    24. };
    25.  
    26. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    i make a function for it actually to make things work :|, but this is for QMap<QString, QString>
    i don't know it's working or not yet!
    Qt Code:
    1. const std::map<std::string, std::string> Aria2c::fromQMap(const QMap<QString, QString> &q_map)
    2. {
    3. std::map<std::string, std::string> std_map;
    4. QMap<QString, QString>::const_iterator i = q_map.constBegin();
    5. while (i != q_map.constEnd()) {
    6. std_map.insert(std::pair<std::string, std::string>(i.key().toStdString(), i.value().toStdString()));
    7. }
    8.  
    9. return std_map;
    10. }
    To copy to clipboard, switch view to plain text mode 

    just for information, it works
    Last edited by Alir3z4; 29th January 2012 at 22:25.
    ...یه مرد هیچوقت زمین نمیخوره

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    This builds correctly:

    Qt Code:
    1. #include <QObject>
    2. #include <QString>
    3. #include <QVector>
    4. #include <QMap>
    5.  
    6. class Downloader : public QObject {
    7. Q_OBJECT
    8. public:
    9. explicit Downloader(QObject *parent = 0) {}
    10.  
    11. const QString addUri(const QVector<QString> &uris);
    12. QString addUri(QVector<QString> &uris, QMap<QString, QString> &options);
    13.  
    14. };
    15.  
    16. #include "main.moc"
    17.  
    18. int main(){
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

    so either your yoDownloaders/aria2c.h is the problem or your building script (.pro file?) is broken.

    Try commenting out the two lines of code concerning aria2c and see if it changes anything.

    By the way, you really shouldn't be passing non-const references to addUri...

    And about your reluctance to show us your code --- you don't have to show us your code... and we don't have to show you ours (nor share our knowledge with you, in the end it's all available on a silver platter in the docs).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Type conversion on container templates

    of course it's working now, because i change it and make it qt/c++
    the downloader class is completely implemented with qt classes,
    this is what i want actually
    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QVector>
    7. #include <QMap>
    8. #include "yoDownloaders/aria2c.h"
    9.  
    10. class Downloader : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit Downloader(QObject *parent = 0);
    15.  
    16. // Operations :|
    17. const QString addUri(const QVector< const std::string(QString::toStdString()) > uris);
    18. QString addUri(const QVector< const std::string(QString::toStdString()) > &uris, QMap<const std::string(QString::toStdString()), const std::string(QString::toStdString())> &options);
    19.  
    20. private:
    21. Aria2c aria2;
    22.  
    23.  
    24. };
    25.  
    26. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 
    I know it's weird, or mybe not

    But the main question is how do make conversion like that. how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>
    By the way i solve my problem with changing the yoDownloaders/aria2c codes and make it work with Qt types.
    ...یه مرد هیچوقت زمین نمیخوره

  14. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Type conversion on container templates

    Your first listing: The error message indicates that at line 17 column 77 the compiler found the error described. None of the lines in this header file are 77 characters long, and none of the lines contain an obvious error. If I replace the aria2c.h include with a null definition of the Aria2c class then the code passes through my compiler no problems. Have you posted the actual code, or just sort-of the code?

    Your second listing: What does this have to do with the posted error?

    Edit: Ignore me...

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    Quote Originally Posted by Alir3z4 View Post
    how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>
    Qt Code:
    1. QVector<QString> qstrvec = ...;
    2. std::vector<std::string> stdstrvec;
    3. foreach(const QString &str, qstrvec) {
    4. stdstrvec.push_back(str.toStdString()); // note, QString is Unicode, std::string is not so the conversion is lossy
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Type conversion on container templates

    i solved my problem somehow, you can check it yodownet/...779995c/log/ if you like ## check the 2 last changes
    but the main question is on also
    how do make conversion like QVector<QString, QString> => std::vector<std::string, std::string>
    Quote Originally Posted by ChrisW67 View Post
    Ignore me...


    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QVector<QString> qstrvec = ...;
    2. std::vector<std::string> stdstrvec;
    3. foreach(const QString &str, qstrvec) {
    4. stdstrvec.push_back(str.toStdString()); // note, QString is Unicode, std::string is not so the conversion is lossy
    5. }
    To copy to clipboard, switch view to plain text mode 
    No, why nobody wanna feels me
    I mean at the function signature

    i made some function already
    https://gist.github.com/1700075
    https://gist.github.com/1700559
    Last edited by Alir3z4; 29th January 2012 at 23:04.
    ...یه مرد هیچوقت زمین نمیخوره

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    I think you need to express your problems in a somewhat more understandable fashion. If your post above says you still have a problem then I can't see where. I don't know what you mean by "I mean at the function signature". Nobody "wanna feels you" at the function signature?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Exclamation Re: Type conversion on container templates

    Quote Originally Posted by wysota View Post
    Nobody "wanna feels you" at the function signature?
    Oh com'on, i make it gray at put that smiley to say it for humor sake!

    how should i express my problem is somehow/somewhat/some??? more understandable fashion?
    i explain it already , make type conversion at the function signature not by another function
    Qt Code:
    1. const QVariant seeMyFace(const QMap<QString, int> ==> std::map<string, int>);
    To copy to clipboard, switch view to plain text mode 
    i mean that little guyz
    Qt Code:
    1. QMap<QString, int> ==> std::map<string, int>
    To copy to clipboard, switch view to plain text mode 
    ...یه مرد هیچوقت زمین نمیخوره

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Type conversion on container templates

    Quote Originally Posted by Alir3z4 View Post
    make type conversion at the function signature not by another function
    I'm sorry but this does not make any sense in terms of exchanging information between two entities. "To do something at the function signature" is not a proper English statement and I can't understand it. You can see that you used such statements twice and you have been misunderstood by at least two distinct people. This should ring a bell in your head that something is wrong with the way you are communicating.

    Qt Code:
    1. const QVariant seeMyFace(const QMap<QString, int> ==> std::map<string, int>);
    To copy to clipboard, switch view to plain text mode 
    i mean that little guyz
    Neither does this make any sense. This is neither C++ code nor pseudo-code nor anything even remotely similar to any meaningful construction in any human-spoken language (at least those I'm aware of).

    So if you want help then please express yourself clearly using full and proper English sentences. This is the best way towards getting understood. If you want an apple then say so instead of saying "I wanna this roundy redish stuff, you know".

    "I mean that little guyz" would suggest you were talking about dwarfs but I can't see anything related to dwarfs in this thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #19
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Type conversion on container templates

    Quote Originally Posted by wysota View Post
    I'm sorry but this does not make any sense in terms of exchanging information between two entities. "To do something at the function signature" is not a proper English statement and I can't understand it. You can see that you used such statements twice and you have been misunderstood by at least two distinct people. This should ring a bell in your head.


    Neither does this make any sense. This is neither C++ code nor pseudo-code nor anything even remotely similar to any meaningful construction in any human-spoken language (at least those I'm aware of).

    So if you want help then please express yourself clearly using full and proper English sentences. This is the best way towards getting understood. If you want an apple then say so instead of saying "I wanna this roundy redish stuff, you know".
    == learn english
    :|
    i solved it, let finish this off
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. Replies: 4
    Last Post: 3rd January 2011, 22:00
  2. conversion between string to hex and vice versa
    By mohanakrishnan in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 11:25
  3. int to String Conversion
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 4th December 2009, 22:43
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. template parameter and conversion operator
    By bitChanger in forum General Programming
    Replies: 7
    Last Post: 21st April 2006, 15:36

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.