QTextEdit and QTextDocument buffer problem? - c++

I have a string. It has about 80000 line.I try to write
QTextDocument * textDocument=new QTextDocument();
textDocument->setHtml(list); //list is my string
txtEdit->setDocument(textDocument);
if string is not contain 80000 line, it can show records.But if has 80000 line, it can not show anything.
Do you have any solution about this problem?
Thanks a lot.

Are you sure it's a problem with the QString itself? Did you tried to output the QString to the console (or called QString::size()) to make sure all the content is stored?
May be it's a limitation that comes from QTextEdit or QTextDocument and not QString.
Also, you could call QString::capacity() to be sure of how much characters you can store in your QString :
int maximumNumberOfChars = list.capacity();

Related

signals and slots with a condition

I have a mainwindow.ui with a QLineEdit_1 and secondWindow.ui with multiple QLineEdit widgets , in short, i wanted to make a condition where when entering a certain number in QLineEdit_1 would result in text output on some of the QLineEdit widgets as "null" and the rest would still be blank.
what are the main steps to achieving the above
if also anyone would lead me to an example similar that would be great
thanks.
Sounds like you are searching for signal-slot-connections: http://doc.qt.io/qt-5/signalsandslots.html.
In your case listen to textChanged() of QLineEdit_1 and create a slot in e.g. secondWindow.ui-class where you set the other line edits as you like:
connect(QLineEdit_1, &QLineEdit::textChanged,
PointerToSecondWindow, &secondWindow::yourSlot);
// In secondWindow.cpp
void secondWindow::yourSlot(const QString &text) {
// Do with text whatever you like and set the
// other line edits.
}

Saving the content of a QLineEdit object into a string variable (C++)

I've looked around the Qt Documentation, but within my project, I'd like to having most of the non-graphical 'more thinking' part of my program be on a seperate .cpp file.
Given that, I was wanting to take the text typed into a QLineEdit object and save it as a string after the user triggers the 'returnPressed' action, but when I type:
void MainWindow::on_lineEdit_returnPressed()
{
QMessageBox msgBox;
msgBox.setText("The entry has been modified.");
msgBox.exec();
//The line which should save the contents of the QLineEdit box:
string input = QLineEdit::text();
}
...Into the template provided by the Qt Creator IDE (with all necessary slots hopefully created) The compiler returns
In member function 'void MainWindow::on_lineEdit_returnPressed()'
cannot call member function 'QString...'
... and so on.
How should I rewrite my code to do this correctly?
You must choose how to store the string. Your main options are: array of chars, std::string from the standard library, and QString from Qt. If you need to use the string in a third party library then you might need to store it in an std::string or an array of chars, but if that's not the case then I suggest that you simply use QString as it is widely used throughout Qt, although you can convert a QString to std::string or array of chars.
You must actually retrieve the text. To do this you must call the text() function on the QLineEdit instance, not on the QLineEdit class itself. All widgets can be accessed through the ui pointer. Open the designer and check the name of the line edit, the default name is lineEdit, so try replacing the line
string input = QLineEdit::text();
with the line
QString input = ui->lineEdit->text();
How about that:
lineEdit->text().toStdString()
For Qt6 this is the best solution that I found
string input = ui->lineEdit->text().toStdString();
A more developed answer from 'alagner'

Can i read value from QLabel

I now how to set value(number) but can i read that label in some variable. if can't read, in which similar widgets can i put value and read it. Any help would be halpful or someone has an other idea. Maybe like LCD number, spin box
Like this?
QString text = someLabel->text();
Here is the documentation. QLabel.text()
EDIT:
QString text = theLabel.text();
//or with pointer
QString text = theLabel->text();
have you tried :
QString s = YourQLabel->text();
This should work, I guess.
// Thelepaty mode on
You need to use editor widgets, for example QLineEdit or QTextEdit. You may customize it via QSS to look like a QLabel.
P.S. try to improve your English. It is hard to understand, what you are asking.
// Thelepaty mode off

store input of line edit to string qt

I'm a beginner.I am making a simple gui program using qt in which you enter a url/website and that program will open that webpage in chrome.I used line edit in which user enters url and i used returnPressed() slot, but the problem is (it might sound stupid) that i don't know how to take the input by user and store it in a string so that i can pass that string as parameter to chrome.Is im asking something wrong.also tell me how can i save input to a txt file, i know how to do that in a console program.Is this process is same with others like text edit etc.
My mainwindow.cpp:
QString exeloc = "F:\\Users\\Amol-2\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
void MainWindow::on_site_returnPressed()
{
QString site;
getwchar(site);
QString space=" ";
QString result = exeloc + space + site;
QProcess::execute(result);
}
What im doing wrong.
thanks
You've got your approach slightly wrong, I can see where you're coming from though. It's actually a lot more simple than you're trying, Qt has a QDesktopServices class that allows you to interact with various system items, including open urls in the browser. There's documentation on it here.
QLineEdit has a text() function that will return a QString. So you can do something like this:
QString site = ui->site->text();
You don't have to use QProcess to open a web site in a browser. You can use QDesktopServices::openUrl static function.
Like this:
QString site = ui->site->text();
QUrl url(site);
QDesktopServices::openUrl(url);
Remember to include QDesktopServices and QUrl headers:
#include <QDesktopServices>
#include <QUrl>

QLineEdit thousand separator

With a QLineEdit is it possible to display the thousand separator of a number while user enter it
Which is the best way to do that ?
You can connect a slot to the void QLineEdit::textEdited ( const QString & text ) signal of your QLineEdit and add some space/separator in the edited string via the setText() method. It should work since textEdit won't be emit-ed again.
The Qt doc says :
Unlike textChanged(), this signal (textEdited) is
not emitted when the text is changed
programmatically, for example, by
calling setText().
You can take advantage of this situation to check if the string entered by the user is actually a number and correct it if needed.