I want a simple example code that shows QLineEdits by a QPushButton click on the same window. i.e. Whenever the button is clicked, a new line edit is displayed on the window.
Or if i should put it this way; if the button is clicked 4 times, 4 line edits should be displayed on that same window
Thank you!
One of the solutions is to create a QVector of pointers to QLineEdit and to add new one each time button is clicked. Each new created QLineEdit has to be added to your window layout. Like this:
QVector<QLineEdit*> lineEdits;
void onButtonClick() {
QLineEdit* newLineEdit = new QLineEdit(this);
lineEdits.push_back(newLineEdit);
// Add newLineEdit to layout
}
Don forget to delete the elements when they're not needed or use smart pointers.
Related
Click the button, a table control appears next to the button, click the blank space or the content in the control to close.
I have two ideas:
With the popmenu, can click the blank space or the content in the control to close the control, but it does not meet my needs for a table-type control
New a tableview, but I don’t know how to accurately appear under the button and realize the function of clicking the blank space or the content in the control to close the control
Update:
I need to use Qt to implement a function, but I don't know how to implement it.
The functions are as follows:
Click a QPushButton, and a QTableView is displayed below the button. I can click anywhere outside the QTableView to close this QTableView
There are two things I cannot achieve:
1.How to display the QTableView below the QPushButton (must be immediately below, such as:
(source: upload.cc)
)
2.How to close the QTableView by clicking the blank space outside the QTableView
If you can make all of this in the same window, it's simple...
To position the QTableView below the QPushButton you will use Qt Designer, and setup the QTableView to hide();. And when the QPushButton is clicked, he setup the QTableView to show();.
An show(); example:
QTableView *nametable = ui->tableView;
nametable->show(); //will appears
But to close or hide(); this when clicked in a blank space I don't know how to help you...
I'm writing code to display labels on the screen and then drag and drop those labels. But when I use show() for those appear on the screen, I create multiple windows, each one of then with one label.
I already tried to use layouts and add a widget in this every time that i click with the mouse on the screen, but i cant place then in a custom position, only side a side or one in the top of the other.
else if(event->button()==Qt::RightButton)
{
QLabel *child = new QLabel;
child->setPixmap(QPixmap("C:/Users/ILHA4/Desktop/boat.png"));
child->move(event->x(),event->y());
child->show();
}
This is from the mousePressEvent() function, and I don't know how to put all these labels that I create when I click on the screen in the same window.
I currently have a QComboBox in a QGraphicsScene and I need it to detect clicks. To see if there is a widget in the clicked position, I use:
void BlockScene::mousePressEvent(QMouseEvent *event)
{
if (itemAt(event->pos()) != m_widgetItem)
{
// ...
}
}
This works well for different widgets except for combo boxes where it only takes into account the original widget and not the drop-down list that appears after a first click.
To know if it came from the scene or not, I tested also by redefining mousePressEvent of the class QComboBox and same problem: It is called only when clicking on the initial widget.
Is there a way to get the drop-down list? To detect a click on it? Ideas?
You can define a custom widget for the view of the combo, or for its completer.
For example, in a subclass of QComboBox, if you need a completer, try this code. MyListView is a subclass of QListView. On that, you can reimplement the mousePressEvent method
completer()->setCompletionMode(QCompleter::PopupCompletion);
MyListView *comboView = new MyListView();
completer()->setPopup(comboView);
if you don't need the completer, do the setView directly on the combobox.
MyListView *comboView = new MyListView();
setView(comboView);
When I press a button, I bring up a dialog where user select things and press 'Ok' at the end. I want a splitter in this dialog. Left pane will show tree and right will show something else. How do I do that right?
From Qt example itself:
QSplitter *splitter = new QSplitter(parent);
QListView *listview = new QListView;
QTreeView *treeview = new QTreeView;
QTextEdit *textedit = new QTextEdit;
splitter->addWidget(listview);
splitter->addWidget(treeview);
splitter->addWidget(textedit);
So in this example, splitter is created without any dialog resource. If I have to create this way, that would mean I have to create all my controls in the code as well rather than Qt Creator.
What is the right way to do this when I need other controls on the screen?
You can simply create splitter containing items in Qt Designer :
First place your widgets on your dialog or widget in designer (They should not be in a layout)
Select the widgets that you want to be in a splitter (By holding CTL and clicking on them)
Right click on a selected widget and from Layout menu select Lay Out Horizontally in Splitter or Lay Out Vertically in Splitter.
Now apply a grid layout to the dialog and everything should be OK. You would see something like this in Object Inspector View :
Okay, I know this is ancient, but here's the complete answer.
First, within some sort of widget container, plop your pieces in. For the window I just did, I have a Widget as my window. I put two widgets inside that labeled something like topContainer and bottomContainer. I then put all the widgets they each need into them, and gave them their own layouts.
Then do NOT select the main container. Select the two widgets you want to split. You're in effect putting a splitter on them, not on the main container. So I went to the widget list window and selected both together, then right-click for the dialog window, scroll down to the Layout option, and "Lay Out Vertically in a Splitter" is NOT greyed out. Select it.
You still need a layout on the main container. A splitter is not a layout. So at that point, I just put a vertical layout on the main container.
To repeat: you are NOT setting a layout on the container holding the pieces you're trying to split. You are selecting the two widgets to split and adding a QSplitter around them. That's the trick to get it to work.
You can still create your controls in a .ui file using Qt Designer (integrated in Qt Creator). Within Qt Designer, add a QWidget object to your dialog. Then, from QDialog derived class you'll write, directly in your constructor, create your QSplitter using the QWidget object as a parent.
This way, you can create all but the splitter object from Qt Designer.
I think it's also possible to create the QSplitter (as you can create a QButton, QCheckBox...) item directly from Qt Designer.
I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.