Results 1 to 18 of 18

Thread: How to present data in a list

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default How to present data in a list

    Hi!
    I have this "Line edit" and a "list View" now I would like to write something in the line edit and when I press enter I would like it to be displayed in the list view.
    I've writen this small line but I have no Idea what I should write for index to be displayed in the list view.

    Qt Code:
    1. void MainWindow::on_lineEdit_2_returnPressed()
    2. {
    3. on_listView_entered(line1->text());
    4.  
    5. }
    6.  
    7.  
    8. void MainWindow::on_listView_entered(QString index)
    9. {
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    best regards

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    To use QListView you need to use Model-View.

    A simple method (that doesn't involve learning Model-View) is to use QListWidget class to display a list in a simple application.
    //it is recommended to learn about Model-View, because it is used in many applications, it's a very useful design pattern.

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    thanks for the tip, what do I write to present it?

  4. #4
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: How to present data in a list

    Something like...

    mainwindow.h:
    Qt Code:
    1. #include <QtGui/QMainWindow>
    2.  
    3. class QLineEdit;
    4.  
    5. class MainWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. protected slots:
    14. void addItemToList();
    15.  
    16. private:
    17. QLineEdit *m_edit;
    18. QListWidget *m_list;
    19. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include <QListWidget>
    2. #include <QLineEdit>
    3. #include <QVBoxLayout>
    4. #include "mainwindow.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent),
    8. m_edit(new QLineEdit),
    9. m_list(new QListWidget)
    10. {
    11. QWidget *widget = new QWidget;
    12. QVBoxLayout *layout = new QVBoxLayout;
    13. layout->addWidget(m_list);
    14. layout->addWidget(m_edit);
    15. widget->setLayout(layout);
    16. setCentralWidget(widget);
    17.  
    18. QObject::connect(m_edit, SIGNAL(returnPressed()),
    19. this, SLOT(addItemToList()));
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. }
    25.  
    26. void MainWindow::addItemToList()
    27. {
    28. m_list->addItem(m_edit->text());
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by helloworld; 1st April 2011 at 22:05.

  5. #5
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    omg that looks extremely complicated...

    The thing is today is the first time I've ever opened Qt so I don't know squat...
    I have this console based app I've written in visual studio that I would like to "connect" to the most basic gui in the easiest way possible and I've heard that qt is the way.

    could anyone here give me some pointers?

  6. #6
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    Ok so I changed to QlistWidget and now I would like to make a function that checks my array (Test**test) and converts all std::strings to QStrings.

    Qt Code:
    1. void Diet::toQString()const
    2. {
    3.  
    4. QString qName(this->name.c_str());
    5. QString qUserName(this->userName.c_str());
    6. QString qDate(this->date.c_str());
    7. //have an int this->kcal
    8. //and have three doubles this->protein, this->carb, this->lipid
    9.  
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    now I have no idea if this is even remotely correct but at least I tried something.

    what I want this function to do is when I call it in listWidget it should present the info from the array from a certain slot.

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    Why don't you use QString from begin to end?

    Anyway QString has some static functions like fromStdString you can use to create QString from std::string.
    Also QString has ways to construct QStrings from int, check the QString documentation.

  8. #8
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    what do you mean by from beginning to end?
    why I have std::string?

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    I mean to use QString instead of std::string all over the place (so that you don't need to convert strings)

  10. #10
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    Yeah I could do that, but then I have to change every single std::string to a QString and I'm almost sure that something will f*** up in the process.
    You think thats the easiest way? easier than only to have a function that converts all strings to QStrings?

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    Well it is easier with conversion, but conversion means a copy of the string and if you have a lot of strings, and that lot increases, at some point that conversion might not be acceptable any more.

    But if your number of conversions is low the performance penalty is also low.

    //but for a new project you can use QString from the beginning and see that QString has many more features than std::string, so it may be easier to use.

  12. #12
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    ok, thanks for the tip.
    What do you think about the function I posted above? Is that conversion working? and what can I write inside that function if I want all the data to be presented in a window.
    I mean if I wanted to do that in VS I would only write cout but what about now?

  13. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    You can still use cout to print QString to console with the help of qPrintable or you can use qstringName.toLocal8Bit().constData();
    Or you can use qDebug()

  14. #14
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    But if I dont want to print it in the console? I want it to be printed out in the QListWidget.

  15. #15
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to present data in a list

    Then use addItem(...)

  16. #16
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    I have these two
    Qt Code:
    1. private:
    2. Ui::MainWindow *ui;
    3. QLineEdit *line1;
    4. QListWidget *list1;
    To copy to clipboard, switch view to plain text mode 

    and these two functions
    Qt Code:
    1. void MainWindow::on_lineEdit_2_returnPressed()
    2. {
    3. on_listWidget_activated(line1->text());
    4.  
    5. }
    6.  
    7. void MainWindow::on_listWidget_activated(QString index)
    8. {
    9. list1->addItem(index);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    but when I run it and type into the lineEdit and push enter nothing happens

  17. #17
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to present data in a list

    Why don't you do yourself and every one else a favor and start with reading some documentation.

    I think you might want to start with the Qt basics like signals and slots.

  18. #18
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to present data in a list

    yeah I understand that but I think that the thing I want to accomplish is not that complicated, so I just need a push in the right direction.
    If I know how to catch data from a line and present some data in a window then I think I can figure out everything else by my self cos I believe its just basic c++ after that.

Similar Threads

  1. How to postpone a signal until present form is closed ?
    By marcvanriet in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2010, 12:22
  2. Replies: 3
    Last Post: 23rd November 2009, 13:41
  3. How to map tree model data to list view
    By msopanen in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 19:56
  4. question about present a picture using QPixmap
    By lovelypp in forum Qt Programming
    Replies: 2
    Last Post: 12th July 2008, 15:43
  5. QFtp->List() does not recieve any data
    By nopalot in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2006, 20:42

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.