Can i read value from QLabel - c++

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

Related

QTextEdit change font of individual paragraph/block

Using a QTextEdit, I need to change the font attributes of each paragraph individually. This is similar to how many word processors change the font of a paragraph when the user select a style from a menu (not a specific formatting).
Ideally, I would like to apply a QTextCharFormat (or equivalent) to a block (paragraph) just before it is laid out and rendered, but I would prefer that no font attribute be actually inserted in the text, as I don't want this information in the file but I need to preserve any bold/italic/underline attributes that the user might have set to words within paragraphs (I intend to save the needed information in a QTextBlock::userData). However, I can't figure where I would need to insert a function to perform this task.
I figured I could not change the QTextCharFormat of a paragraph from either QTextBlock nor QTextCursor as this only applies to new blocks, it doesn't affect blocks with existing text.
I checked out QTextLayout but I don't think my answer is there.
I have been looking for a solution to this problem for a few days now. I would be really gracious for any pointer in the right direction.
I have years of experience with C++, but I'm somewhat new to Qt. Using Qt 4.8.
Edit:
I added emphasize (bold) above to an important part of what I'm trying to do. In other word, what I'd really like to do is be able to apply the font attributes to the block of text (perhaps a temporary copy) just before it is displayed. I'm totally comfortable with deriving and modifying (even reimplement) any class that I need to in order to achieve that goal, but I need to be pointed to the right direction as to what I actually need to change. As a last resort, I could also modify some Qt class directly if that is necessary for the task, but again would need to know what class I need to touch. I hope this is clearer. I find it difficult to explain this without being allowed to tell you what the application will do exactly.
[Required Libraries]
#include <QTextEdit> // not needed if using the designer
#include <QTextDocument>
#include <QTextBlock>
#include <QTextCursor>
[Strategy]
QTextDocument
I need it to manage the blocks. The function QTextDocument::findBlockByNumber is quite handy to locate the previous blocks, and I think it is what you are after.
QTextBlock
Container for block texts. A nice and handy class.
QTextCursor
Surprisingly, there is no format-setter in QTextBlock class. Therefore I use QTextCursor as a workaround since there are four format-setters in this class.
[Code for formatting]
// For block management
QTextDocument *doc = new QTextDocument(this);
ui->textEdit->setDocument(doc); // from QTextEdit created by the Designer
//-------------------------------------------------
// Locate the 1st block
QTextBlock block = doc->findBlockByNumber(0);
// Initiate a copy of cursor on the block
// Notice: it won't change any cursor behavior of the text editor, since it
// just another copy of cursor, and it's "invisible" from the editor.
QTextCursor cursor(block);
// Set background color
QTextBlockFormat blockFormat = cursor.blockFormat();
blockFormat.setBackground(QColor(Qt::yellow));
cursor.setBlockFormat(blockFormat);
// Set font
for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
{
QTextCharFormat charFormat = it.fragment().charFormat();
charFormat.setFont(QFont("Times", 15, QFont::Bold));
QTextCursor tempCursor = cursor;
tempCursor.setPosition(it.fragment().position());
tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
tempCursor.setCharFormat(charFormat);
}
Reference:
How to change current line format in QTextEdit without selection?
[DEMO]
Building Environment: Qt 4.8 + MSVC2010 compiler + Windows 7 32 bit
The demo is just for showing the concept of setting the format on a specific block.
Plain text input
Format 1 (notice that it won't bother the current cursor in view)
Format 2
You can use QTextCursor to modify existing blocks.
Just get a cursor and move it to the beginning of the block. Then move it with anchor to create a selection.
Set this cursor to be the current cursor for the text edit and apply your changes.
QTextEdit accepts HTML so all you have to do is to format your paragraphs as HTML. See example below:
QString text = "<p><b>Paragraph 1</b></p><p><i>Paragraph 2</i></p>";
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertHtml(text);
That will create something like this:
Paragraph 1
Paragraph 2
Having said that, there is only a subset of HTML that is supported in Qt. See Supported HTML Subset

Getting wxStaticBitmap in BoxSizer

I try to ask not to localized, because I think many other wxWidgets users will stumble about this problem or a similar one.
I try to get a wxStaticBitmap (original a PNG-Image) in a GUI, I will try to model it:
text text text
text text text
IMAGE
text
text
How I can do this? When I try:
wxBitmap bild(_T("Bild.png"), wxBITMAP_TYPE_PNG);
wxStaticBitmap *image = new wxStaticBitmap(this, wxID_ANY,
wxBitmap("Bild.png", wxBITMAP_TYPE_PNG),
wxPoint(100,100),
wxSize(bild.GetWidth(), bild.GetHeight()));
The image will take over the hole window, even the wxNotebook, which contains the wxPanel, will disabled and I have only the image, but nothing else...
I want to get the image in the third row of the VerticalBox. Is there any way to make this happend?
-Casisto
(wxW: 2.9.4; C++-Compiler: g++)
There is nothing special about wxStaticBitmap and you absolutely don't need to put it inside a panel. You do need to create it with the correct parent, if this in your example refers to wxFrame, it's not going to work if you then put it inside a wxPanel sizer, for example.
P.S. Do get rid of _T in your code, it's completely unnecessary in 2.9+. You also probably don't want to load the bitmap twice...
Ok, i found the answer myself:
I've to use a "subpanel" which will be the parent of the StaticBitmap
so it's this structur:
panel << vbox << Text
vbox << panel_2 << Image
vbox << Text

What is the corresponding code in Qt for "string s = textBox1.text"?

How can I read a text from "textEdit" tool of Qt gui applicatons?
I was expecting something like QString s = ui->textEdit->text();
but there is no command like this.
QTextEdit contains method:
QString toPlainText()
That should give you the text contained in the QTextEdit.

Making QLabel behave like a hyperlink

how can I make a QLabel to behave like a link? What I mean is that I'd like to be able to click on it and then this would invoke some command on it.
QLabel does this already.
Sample code:
myLabel->setText("Click Here!");
myLabel->setTextFormat(Qt::RichText);
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
myLabel->setOpenExternalLinks(true);
The answer from cmannnett85 is fine if you just want to open a URL when the link is clicked, and you are OK with embedding that URL in the text field of the label. If you want to do something slightly custom, do this:
QLabel * myLabel = new QLabel();
myLabel->setName("myLabel");
myLabel->setText("text");
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
Then you can connect the linkActivated signal of the label to a slot, and do whatever you want in that slot. (This answer assumes you have basic familiarity with Qt's signals and slots.)
The slot might look something like this:
void MainWindow::on_myLabel_linkActivated(const QString & link)
{
QDesktopServices::openUrl(QUrl("http://www.example.com/"));
}

QTextEdit and QTextDocument buffer problem?

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();