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.
Code:
void MainWindow::on_lineEdit_2_returnPressed()
{
on_listView_entered(line1->text());
}
void MainWindow
::on_listView_entered(QString index
) {
}
best regards
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.
Re: How to present data in a list
thanks for the tip, what do I write to present it?
Re: How to present data in a list
Something like...
mainwindow.h:
Code:
#include <QtGui/QMainWindow>
{
Q_OBJECT
public:
~MainWindow();
protected slots:
void addItemToList();
private:
};
mainwindow.cpp:
Code:
#include <QListWidget>
#include <QLineEdit>
#include <QVBoxLayout>
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
layout->addWidget(m_list);
layout->addWidget(m_edit);
widget->setLayout(layout);
setCentralWidget(widget);
QObject::connect(m_edit,
SIGNAL(returnPressed
()),
this, SLOT(addItemToList()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::addItemToList()
{
m_list->addItem(m_edit->text());
}
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?
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.
Code:
void Diet::toQString()const
{
QString qUserName
(this
->userName.
c_str());
//have an int this->kcal
//and have three doubles this->protein, this->carb, this->lipid
}
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.
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.
Re: How to present data in a list
what do you mean by from beginning to end?
why I have std::string?
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)
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?
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.
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?
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()
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.
Re: How to present data in a list
Re: How to present data in a list
I have these two
Code:
private:
Ui::MainWindow *ui;
and these two functions
Code:
void MainWindow::on_lineEdit_2_returnPressed()
{
on_listWidget_activated(line1->text());
}
void MainWindow
::on_listWidget_activated(QString index
) {
list1->addItem(index);
}
but when I run it and type into the lineEdit and push enter nothing happens :(
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.
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.