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");
Related
Good morning, I am learning about programming and GUI in Qt c++ and I have some doubts:
Let's suppose you have this GUI shown in the picture. No worries about how has it been created. It has:
QTreeWidget
QLineEdit
QPushButton1
QPushButton2
How would you make the following things?
When clicking on a name or in a value, to make that only the value of this row appear in the QLineEdit
In the next example, I have been able to show the name or the value, but I only want the value to be shown, even if the name has been clicked.
void MainWindow::on_treeView_activated(const QModelIndex &index)
{
QString val = ui->treeView->model()->data(index).toString();
ui->lineEdit->setText(val);
}
If I change the value of the QLineEdit and then click the QPushButton1, the QTreeWidget should be updated with this new value.
If I click the QPushButton2, save "the names = the values" into a .txt file
(FUTURE) I want to have these 3 point clear first. Then, my goal is to make the same with this picture:
Thanks a lot in advance!
I used a QFileDialog to open a browser.
Here is my code:
QString filePath = QFileDialog::getSaveFileName(this,
"Export Xml", "PluginPythonQt",
"Xml files (*.xml)");
When excute it will show a dialog like this:
I want to disable the "File name:" box in the picture or prevent user to enter a new name. How can i do that ? Thanks.
I believe you can't achieve this — save dialog is about choosing name besides the choosing where to save it. Of course, you might just ignore what user typed and force your name when he hits OK but it will just make the user angry.
Better way, in my opinion, is to use QFileDialog::getExistingDirectory which will allow the user to choose where to save the file but won't allow him to choose the file name. It will be fair, at least.
Similar question was answered in https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box.
In general, you can hide any element in any widget if you dig a bit into widget's source code to find element's name, when you have a name, you can find the corresponding element via findChild<QWidget *>(elementName).
Usually if you check QSomeWidget.h (Qt is open source!) you can find element names very easily as they are typically listed as the widgets members.
To hide both labels, fileEdit, ComboBox and even buttons, you can use this code:
QFileDialog fileDialog = new QFileDialog;
QWidget * fileNameEdit = fileDialog->findChild<QWidget *>("fileNameEdit");
Q_ASSERT(fileNameEdit);
fileNameEdit->setVisible(false);
QWidget * fileNameLabel = fileDialog->findChild<QWidget *>("fileNameLabel");
fileNameLabel->setVisible(false);
QWidget * fileTypeCombo = fileDialog->findChild<QWidget *>("fileTypeCombo");
Q_ASSERT(fileTypeCombo);
fileTypeCombo->setVisible(false);
QWidget * fileTypeLabel = fileDialog->findChild<QWidget *>("fileTypeLabel");
fileTypeLabel->setVisible(false);
QWidget * fileButtonBox = fileDialog->findChild<QWidget *>("buttonBox");
fileButtonBox->setVisible(false);
Note that even though buttons are hidden, typing Enter on keyboard (or double clicking) would trigger Open button, and dialog might disappear if you haven't done anything in Accept method. So it would also be a good idea to handle state of that button as well if you really wish buttons to be hidden as well.
I am using qt to develop an embedded gui application. I am using 2 QStatusBars to make a menu-like buttons one can see on an osciloscope for example:
My problem is I dont know a proper way of separating the buttons from eachother with a certain width. In the picture you can see I have added couple separators to achieve that, but it doesnt look that way when run on the target.
Is there a better way to separate buttons on QStatusBar with certain width?
I'd prefer you use a blank widget to do the seperation as suggested by Martin, like so;
//the 2 widgets in the status bar
button1 = new QPushButton("Button1");
button2 = new QPushButton("Button2");
//the blank widget. You can set your width with 'setFixedWidth(int)'
widget = new QWidget;
widget->setFixedWidth(50);
widget->setHidden(1);
widget->setVisible(1);
//placing them in the status bar
statusBar = new QStatusBar;
statusBar->addWidget(button1);
statusBar->addWidget(widget);
statusBar->addWidget(button2);
I would like to create a windows application that displays a window based on a existing text file. For example, I may have a text file with the following information:
window_size 400, 300
push_button 2
radio_button 5
My program should be able to read this text file and create a window of 400 x 300 pixels, with 2 push buttons and 5 radio buttons. Of course, the content of the text file may change and is unpredictable. Please ignore at this moment other nessesary data, like the position and size of the buttons, I just need to know if its possible to do this, and a general idea on how to do it.
I'm using Qt with C++. I may change Qt if there is a better option to do this, but I must stick with C++.
You should parse and set accordingly.
Create a QWidget, parse the first line of the text file, set its size. Then parse next lines to get the number of buttons, create widgets dynamically. Something like:
// main
// ...
QString geoLine = stream.readLine();
int width = geoLine.split(" ")[1].toInt();
int height = geoLine.split(" ")[2].toInt();
QWidget * widget = new QWidget();
widget->setGeometry(0, 0, width, height);
// create a layout for the child widgets, then create and add them dynamically.
QVBoxLayout * layout = new QVBoxLayout();
int nButtons = stream.readLine().split(" ")[1];
// full control of dynamically created objects
QList<QPushButton *> buttons;
while(nButtons > 0) {
QPushButton * button = new QPushButton();
buttons.append(button);
layout.addWidget(button);
nButtons--;
}
// same for radio buttons
// ...
widget->setLayout(layout);
widget->show();
// ... etc, app exec,
qDeleteAll(buttons);
delete widget;
return 0;
If you want to learn the widget type from push_button or radio_button directives; you must switch - case onto those parsed strings.
There is also completely another way. You can create a form (.ui) file using an XML data. You must create a ui class (like the template designer form class created by Qt), and create its .ui file according to your text file - parsed and converted to a proper XML.
As far as I know Qt handles the widget creations using that XML information, and generates the file ui_YourClass.h..
As canberk said you can use native Qt UI file format .ui by usage of QUiLoader class - see reference http://doc-snapshot.qt-project.org/qt5-5.4/quiloader.html
Hopefully a very quick question. In one of my functions I want to generate an 'x' number of tabs for a QTabWidget during run-time ('x' provided by user). I know I have to use the addTab function for the QTabWidget (correct me if I'm wrong), but I am unsure as to how I am supposed to use it. The qt documentation was unclear to me.
I have tried the following command to add only 1 tab as a test, but it caused the program to crash:
ui->checkBoxTabArea->addTab(ui->checkBoxTabArea,"tab2");
Since I am obviously wrong, can somebody help me use this function? I feel like I am just passing the wrong arguments to addTab.
Edit: checkBoxTabArea already has 1 tab when the program starts up (if this is any help).
If you take a look at the documentation, the addTab function says this :
int QTabWidget::addTab ( QWidget * page, const QString & label )
Adds a tab with the given page and label to the tab widget, and
returns the index of the tab in the tab bar.
So the argument you pass should not be the TabWidget you want to add the tab to, rather it should be the widget you want to add as the tab.
What you should do is something like :
QLabel *myLabel = new QLabel("Hello World", this);
ui->checkBoxTabArea->addTab(myLabel, "My Label Tab");
This will add a single tab to the tab widget, which contains myLabel.