Adding/Removing Qlineedit objects during runtime with dependency to QCombobox object
-
Hi all,
I have a simple benchmark study:
I have a mainwindow application. It only consists of a Qcombobox instance (with 3 items) and 3 Qlineedit instance within a vertical layout. My purpose is:
When the user select 1st item of the combobox is selected(default behavior) , all 3qlineedit objects are visible but disabled.
When the 2nd combobox item is selected, only the 1st qlineedit object would be visible and enabled (with preserving its position and size in the vertical layout), the last two qlineedit objects would disappear.
When the 3rd combobox item is selected, the 1st & 2nd qlineedit object would be visible and enabled (with preserving their positions and sizes in the vertical layout), the 3rd qlineedit object would disappear.
I've tried lots of options but encountered with run time errors. What kind of ways, classes or functions I should use? Or do you have any simple code similar to this subject? Isearched all the forum(s) but didn't see any solution.
Thanks in advance
-
Hi,
What run time errors are you getting ? What code do you use ?
-
Thanks,
Actually, I tried to solve the problem by trying to delete default layout and inserting new Qlineedit objects for selection of combobox object's each item. But it seems to be complicated?
Then, I changed my mind and solved the problem by using hide() and show() functions. Or do you suggest another way?
But, I still have location problems for lineedit objects in the layout. They are always changed depending on the selection. What would you suggest to fix this issue?
Also,I have image overlay issue for each selection. When, I changed the selection, the images are overlapped. I would like to see only one image per selection. (Should I open a new topic for this issue or would we go on from this topic?)
Also, would you tell me sth about the design approach of this application?
Thanks in advance...
@//main.cpp
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}@
@//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_comboBox_currentIndexChanged(int index);private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsView *view;
};#endif // MAINWINDOW_H
@@//mainwindow.cpp
#include <QGridLayout>
#include <QGraphicsItem>
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(ui->centralWidget);
view = new QGraphicsView(scene);
QGridLayout *gLayout = new QGridLayout(ui->centralWidget);
QVBoxLayout *vLayout = new QVBoxLayout();
gLayout -> addWidget(ui->comboBox,0,0,1,2);
gLayout -> addLayout(vLayout, 1,0,3,2);
gLayout ->addWidget(view,0,2,3,3);ui->lineEdit->setEnabled(false); ui->lineEdit_2->setEnabled(false); ui->lineEdit_3->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_comboBox_currentIndexChanged(int index)
{
QGraphicsPixmapItem *item;if(index == 0){ ui->lineEdit->hide(); ui->lineEdit_2->hide(); ui->lineEdit_3->hide(); item = new QGraphicsPixmapItem(QPixmap("C:\\... jpg")); scene->addItem(item); view->show();} else if(index == 1) { ui->lineEdit->show(); ui->lineEdit_2->show(); ui->lineEdit_3->hide(); ui->lineEdit->setEnabled(true); ui->lineEdit_2->setEnabled(true); item= new QGraphicsPixmapItem(QPixmap("C:\\... jpg")); scene->addItem(item); view->show();} else if(index == 2){ ui->lineEdit->show(); ui->lineEdit_2->hide(); ui->lineEdit_3->hide(); ui->lineEdit->setEnabled(true); item = new QGraphicsPixmapItem(QPixmap("C:\\... jpg")); scene->addItem(item); view->show();}
}
@ -
Using hide and show is imho the right way.
As for the pixmap problem, you don't set their position so they will go to their default position. The other thing is that you recreate the item each time, are you sure that's what you want ?
-
Thanks,
Instead of their positioning, I'm suffering about overlay problem. I want to connect an image file for each combobox item. But, when I change the combobox item each image overlays onto another. It wouldn't be like that.
Yes, I create them each time but I should delete them. I think, to delete them I need to check each graphic item. But to do this, I think I need to use an abstract class.
What is your idea?
Also, I 've opened a new topic "Image overlay issue during run-time". If you want you would post your answers to there.
-
The answers given by 1+1=2 are good you should follow them