Solved QString to char*
-
@Dejavu Hi, and welcome to the Qt forum!
#include <QString> #include <QList> #include <QDebug> #include <QPair> typedef QPair<QChar, int> YourPair; typedef QList<YourPair> YourList; YourList computeYourHomework(const QString &s) { QList<QChar> list; for (int i=0; i<s.size(); i++) { const QChar c(s.at(i)); if (!list.contains(c)) { list << c; } } YourList result; for (int i=0; i<list.size(); i++) { const QChar c(list.at(i)); const int count = s.count(c); result << YourPair(c,count); } return result; } void printYourHomework( const YourList &list ) { for (int i=0; i<list.size(); i++) { const YourPair pair = list.at(i); qDebug() << QString("'%1' - %2").arg(pair.first).arg(pair.second); } } int main(int argc, char *argv[]) { Q_UNUSED(argc) Q_UNUSED(argv) const YourList result = computeYourHomework("bodya_krasavka_epta"); printYourHomework(result); return 0; }
Cheers!
-
Thank's a lot for help. How i can get a access to values in QList to edit characters or counter?
-
See here http://doc.qt.io/qt-5/qlist.html
list[i] = ...
-
Thanks a lot to all and thank you so much @Wieland
-
@Dejavu You're welcome ;-)
-
@Wieland sorry for new stupid question)
When I try to display the text in textEdit I got this error: C2227: left of '->textEdit' must point to class/struct/union/generic typevoid printYourHomework( const YourList &list)
{
double *mynum, *freq, diap;
double *cum_freq;
double temp_cum = 0.0;
for (int i=0; i<list.size(); i++)
{
mynum = new double[i];
freq = new double[i];
cum_freq = new double[i];
}
for (int i=0; i<list.size(); i++) {
const YourPair pair = list.at(i);
MainWindow::ui->textEdit->append(QString("'%1' - %2").arg(pair.first).arg(pair.second));
mynum[i] = pair.second;
freq[i] = mynum[i]/18;
MainWindow::ui->textEdit->append(QString::number(freq[i], 'f', 8));
} -
@Dejavu said:
MainWindow::ui->textEdit->
That might be the reason.
You cannot access mainwindows UI outside of mainwindow.So easy fix is to move the functions to mainwindow
( remember to add it to the mainwin.h file too)YourList MainWindow::computeYourHomework(const QString &s) {
..
ui->textEdit->append(xxx
} -
@mrjj thank you for the reply
I add it to mainwin file:private slots:
YourList computeYourHomework(const QString &s);
void printYourHomework(const YourList &list);and then i received errors:
http://prntscr.com/b2hmp6I add function in mainwindow.cpp.
-
@Dejavu
Hi
Maybe dont list it as slots. ( not the real error)
just put it under
private:Then u also need to make sure it knows
YourList type
so u must move that over mainwindow class
or put in its on file and
#include "YourList .h" -
@mrjj thank u so much.
I fix this problem. I just add #include in header and now it works.
Thanks a lot :) -
@Dejavu
Super :)