How Qcombobox change text of a line edit - c++

In a user setting page, in that window i want a combobox that once you have selected something it will change the text of a line edit. for example
someone chose patrick and than the text change to his address and phone number.
im suspecting the code will look something like this:
if Qcombobox == "patrick"{
QlineEdit_phone = "911"
}
i have started to use QT designer and im lost on how Iam connecting slot and signal with object also.

the slot to change the editline by the change of selection through a combobox look like:
void pagesetting::on_comboBox_activated(const QString &arg1)
{
if (arg1=="gmail"){
ui-> lineEdit_port -> setText("465");
ui -> lineEdit_host -> setText("smtp.gmail.com");
}
if (arg1=="yahoo"){
ui-> lineEdit_port -> setText("465");
ui -> lineEdit_host -> setText("smtp.mail.yahoo.com");
}
}
if you use qt designer you still need to delcare everything if you want to play with the object

Related

Changing qt5 tab names dynamically

Say I have a tabwidget in my ui file
this is how im adding tabs right now:
QPlainTextEdit *tab = new QPlaintextEdit;
int index = ui->tabWidget->addTab(tab, "changeme");
Now I'm wondering if it's possible to change the name of the tab on the go,
for example when subclassing QPLainTextEdit in a class and connecting a signal to it when the text changes then i'd like to add a little star to the tab to indicate that the file has been modified, is it even possible?
QTabWidet::setTabText does what you want.
E.g:
ui->tabWidget->setTabText(index, "new text");

QT setting text of lineEdit

I am trying to learn QT and wanted to set the text of a lineEdit when pressing a push button. I tried using this code
void Notepad::on_pushButton_10_clicked(){
Notepad::lineEdit -> setText("test");
}
but it does not work since lineEdit is not a member of Notepad. How do I reference lineEdit?
Your code will look something like below :
void Notepad::on_pushButton_10_clicked(){
ui->lineEdit->setText("test");
}
provided that you named the lineEdit Widget as the lineEdit.

How to catch information for Qt designer

I have created a Qdialog box using the Qt creator designer as shown below:
When I need to display it, I'm instantiate the class dialogoverwrite (.cpp, .h and .ui)
DialogOverwrite *OverwriteDialog = new DialogOverwrite;
OverwriteDialog->exec();
OverwriteOption = OverwriteDialog->result()
My issue is that I want to get the QDialogButtonBox result but I do not know how. the current code, returning the result of the OverwriteDialog but it's not returning any QDialogButtonBox::Yes, QDialogButtonBox::YesToAll ...
How to catch the QButtonGroup result and not the QDialog result.
In the same way, If I want to change the label value from "File(s) and/or Folder(s)" to another label, how to access to this QLabel ?
Thanks for your help
When you pressed QDialogButton it was emit signal clicked(QAbstractButton*) by catching this signal you can identify which action button pressed.
Please go through following link it would be help you.
Qt: How to implement QDialogButtonBox with QSignalMapper for non-standard button ??
Well the standard way to do this is to handle the result by connecting it. So you could do:
connect(this, SIGNAL(clickedDialogButton(QAbstractButton*)),
SLOT(dialogButton(QAbstractButton* aButton)));
Next you would create a function in your class called dialogButton (for example) and have that handle the result:
void MyUI::dialogButton(QAbstractButton* aButton) {
// Obtain the standard button
StandardButton button = buttonBox−>standardButton(button);
// Switch on the type of button
switch (button) {
case QDialogButtonBox::YesToAll:
// Do the thing you would like to do here
break;
// add some more cases?
}
}
You could also check for the signal given by the QButtonGroup. Something like: void QGroupButton::buttonClicked(QAbstractButton* button) would work in the same way.

How to show list of strings in QT at run time?

I have a list of strings during run time.
Any one help me to display these strings in QWidget. When I right click that string I need to have a option show index that will show index of that string in QMessageBox.
If it is possible means give some technical guidance.
Thank you.
OK, so let us start with the design for your use case...
I would recommend using a QListWidget for a list. Each string could be a separate item.
You could show a popup for right click, but if it only has the show index action, it does not really make that much sense on its own. You could just show the messagebox with that index right away.
I would write something like this below:
MyClass::MyClass(QObject *parent)
: QObject(parent)
, m_listWidget(new QListWidget(this))
{
QStringList myStringList = QStringList() << "foo" << "bar" << "baz";
m_listWidget->addItems(myStringList);
// Set up your action with the popup for right click if needed
// and connect to the "triggered" signal of the particular action
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem * item)), SLOT(showMessageBox(QListWidgetItem * item)));
...
}
void MyClass::showMessageBox(QListWidgetItem * item)
{
Q_UNUSED(item)
QMessageBox messageBox;
messageBox.setText(m_listWidget->currentRow());
messageBox.exec();
}
If you have more actions for the right click, you could use a popup dialog, yes, with several actions placed onto, but so far, that does not seem to be the use case in here.

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/"));
}