So the question is how do I set a value of textEdit from another form?
You have two options, either you can simply call one of the setText functions from a function within another form like this:
otherForm->setPlainText(text);
Or you could connect the two forms with signals like this:
connect(form1, SIGNAL(updateText(const QString&)),
form2->myTextEdit, SLOT(setText(const QString&)))
Either of these are valid ways to do it.
myTextEdit->setPlainText(text), myTextEdit->setHtml(text) or by directly modifying the editor's QTextDocument instance.
Related
I'm trying to connect a Slider and LineEdit widget together so that when one is changed the other will match this value.
I'm struggling since lineEdit takes strings and Slider takes ints. I already used setValidator on the lineEdit so that only ints can be entered.
I tried using the old signals and slots syntax with no luck using a couple different methods from a quick Google search.
connect(textbox, SIGNAL(textEdited(QString)),slider,
SLOT((QString)));
Should I be using a different widget entirely than LineEdit?
Use the lineEdit() method of QSpinBox():
connect(textbox, &QLineEdit::textEdited, slider->lineEdit(), &QLineEdit::setText);
I'm using stackoverflow firsty to answer others' question,Hahaha.
The better binding signal for QSlider is QSlider::sliderMoved, and do not bingding QSlider::valueChanged, otherwise they will conflict, maybe you can try it.
Binding QLineEdit::textChanged signal for QLineEdit.
code here:
connect(ui->horizontalSlider,&QSlider::sliderMoved,this,[=]{
int val=ui->horizontalSlider->value();
//You can process the variable val here.....
ui->lineEdit->setText(QString::number(val));
});
connect(ui->lineEdit,&QLineEdit::textChanged,this,[=]{
int val=ui->lineEdit->text().toInt();
//You can process the variable val here.....
ui->horizontalSlider->setValue(val);
});
I understand that we could use QSignalMapper to collect a set of parameterless signals, and re-emit them with integer, string or widget parameters corresponding to the object that sent the signal.
But could we do the reverse?
For example, is it possible to achieve:
connect(control,startVehicle(0),vehcileList[0],startReceived());
connect(control,startVehicle(1),vehcileList[1],startReceived());
connect(control,startVehicle(2),vehcileList[2],startReceived());
instead of having 3 different signals from control as
startVehicle_1();
startVehicle_2();
startVehicle_3();
There is a simpler way:
connect(control, SIGNAL(startVehicle(int)), this, SLOT(startReceived(i)));
//in startReceived(i) slot
vehcileList[i]->startReceived();
The idea was to connect QWidget with a variable so that when text changes on a widget it will be also changed in a variable.
And do this with just one line like this
WidgetMapper::connect(ui->lineEdit, SIGNAL(textChanged(QString)), someClass.var);
which would connect for example QLineEdit with a variable.
1) This would display var in a lineEdit
2) when lineEdit fires an textChanged(QString) signal - WidgetMapper would convert this QString to correct mapped type with stringstream and write it to var.
But i dont really know templates that well, and dont know if it is possible at all.
I dont think it is possible to use one WidgetMapper for every type, so i also tried creating separate instances for each type (WidgetMapper<int> mapper;) which would still be betten then writing setters and onTextChangedSlots for each QLiteEdit but i could not figure out how to make it work as well (converter part still could not figure out the correct type).
WidgetMapper is using QSignalMapper to map signal to QWidget, and it worked fine, the part i have troubles with - is converting QString to template variable.
So is it possible? And if yes how could i do this? Or maybe there already a solution for this problem? (Somehow use QDataWidgetMapper with a class that contains variables maybe?)
First of, connecting the variable would do nothing else than calling some function if it were possigle.
Second try using QSignalMapper, this way you could use a single slot for all widgets, given you keep their pointers in an array with the index being the signal(int) emitted by the SignalMapper. This way your slot can just use MyWidgetArray[i]->text().
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/"));
}
How to set a specific style to a certain QMessageBox in Qt. Particularly, I would like to invoke a message box which is written below by setting a font size and color for the text "Well done!!!".
QMessageBox::information(this, "Done", "Well done!!!");
Using this static method you can not access QMessageBox directly, because its created and destroyed entirely inside function. But you may try to alter stylesheet property of calling object before call to this function. This means calling object should be descedant of QWidget...
something like this:
QString tmpStyleSheet=this->styleSheet();
this->setStyleSheet("style sheet for your QMessageBox");
QMessageBox::information(this, "Done", "Well done!!!");
this->setStyleSheet(tmpStyleSheet);
style of QMessageBox will inherit parent's style...
You will need to create a QMessageBox object for that particular message box and then either setStyle() or setStyleSheet(), in other words you cannot use the static helper method.
I have formated the text of the QMessageBox with HTML code. Sorry for the question. And thanks for answers.