qt why qlineedit hidden after resize?
-
Why qlineedit in qlistwidgetitem hidden after resize?
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 <QListWidget> #include <QLineEdit> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> #include <QTreeWidgetItem> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void makeMagic(); void addNewElement(QString name); QHBoxLayout* qhbl; QListWidget* qlw; QLineEdit* qle; private slots: //void test(QListWidgetItem *item); void showContextMenuForWidget(const QPoint &pos); //void Validate(QListWidgetItem*); void addNewElement(); void editElement(); void deleteElement(); void qleTextChanged(QString); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H mainwindow.cpp #define _old #include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> #include <QPushButton> #include <QAction> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); makeMagic(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::makeMagic() { qhbl = new QHBoxLayout(); qlw = new QListWidget(); qlw->setViewMode(QListWidget::IconMode); //qlw->setFlow(QListWidget::LeftToRight); //qlw->setWrapping(true); qlw->setMovement(QListWidget::Static); //qlw->setGridSize(QSize(80,80)); qlw->setResizeMode(QListWidget::Adjust); //border-style: solid; QString qstrStyle = "QListWidget::item { "; qstrStyle += "border-radius: 10px;"; qstrStyle += "margin: 1px;"; qstrStyle += "min-width: 10em; min-height: 0.75em; padding: 1px;"; qstrStyle += "background-color: rgb(187,204,241);" ; qstrStyle += "border-style: outset; border-width:1px; border-color:black;"; qstrStyle += "}"; qstrStyle += "QListWidget::item::selected {"; qstrStyle += "background-color: rgb(136, 158, 207);"; qstrStyle += "border-style: inset; color: white; border-width:1px;"; qstrStyle += "border-color:black; "; qstrStyle += "}"; qlw->setStyleSheet(qstrStyle); //qlw->setStyleSheet(" QListWidget {background-color: rgb(63, 63, 63);} "); connect(qlw, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenuForWidget(const QPoint &))); qlw->setContextMenuPolicy(Qt::CustomContextMenu); addNewElement("test111111111"); addNewElement("test222222222"); addNewElement("test3333"); addNewElement("test4444"); #ifdef good QListWidgetItem* qlwiItem = new QListWidgetItem(); qlwiItem->setTextAlignment(Qt::AlignHCenter); qle = new QLineEdit(); qle->setText("input"); connect(qle, SIGNAL(returnPressed()), this, SLOT(addNewElement())); connect(qle, SIGNAL(textChanged(QString)), this, SLOT(qleTextChanged(QString))); //qle->setFixedWidth(70); qle->resize(qlwiItem->sizeHint().width(), qlwiItem->sizeHint().height()); //qle->setMinimumWidth(qlwiItem->sizeHint().width()-10); //qle->setMaximumWidth(80); //qle->setAlignment(Qt::AlignBaseline); #endif QListWidgetItem* qlwiItem = new QListWidgetItem(); qlwiItem->setTextAlignment(Qt::AlignHCenter); qle = new QLineEdit(); qle->setText("input"); connect(qle, SIGNAL(returnPressed()), this, SLOT(addNewElement())); connect(qle, SIGNAL(textChanged(QString)), this, SLOT(qleTextChanged(QString))); //qle->setFixedWidth(70); //qle->resize(qlwiItem->sizeHint().width(), qlwiItem->sizeHint().height()); qle->setMinimumWidth(qlwiItem->sizeHint().width()-10); //qle->setMaximumWidth(80); //qle->setAlignment(Qt::AlignBaseline); qlw->addItem(qlwiItem); qlw->setItemWidget(qlwiItem, qle); #ifdef _old qhbl->addWidget(qlw); #else qhbl->addWidget(new QPushButton(tr("Short"))); qhbl->addWidget(new QPushButton(tr("Longer"))); qhbl->addWidget(new QPushButton(tr("Different text"))); qhbl->addWidget(new QPushButton(tr("More text"))); qhbl->addWidget(new QPushButton(tr("Even longer button text"))); #endif ui->centralWidget->setLayout(qhbl); } void MainWindow::qleTextChanged(QString) { // QString text = qle->text(); // QFontMetrics fm = qle->fontMetrics(); // int w = fm.boundingRect(text).width(); // qle->resize(w, qle->height()); } void MainWindow::addNewElement(QString name) { QListWidgetItem* qlwiItem = new QListWidgetItem(); //qlwiItem->setBackgroundColor(Qt::yellow); QStringList qslname = name.split(";"); for(int i=0; i<qslname.count(); i++) { qlwiItem->setText(qslname.at(i)); qlwiItem->setFlags(qlwiItem->flags () | Qt::ItemIsEditable); qlw->addItem(qlwiItem); } } void MainWindow::showContextMenuForWidget(const QPoint &pos) { QListWidgetItem* widget = qlw->itemAt(pos); if(widget != 0) { if(widget == qlw->item(qlw->count()-1)) return; QMenu contextMenu("Контекстное меню", this); QAction* actChange = contextMenu.addAction(QString::fromLocal8Bit("Изменить")); connect(actChange,SIGNAL(triggered()),this,SLOT(editElement())); QAction* actDelete = contextMenu.addAction(QString::fromLocal8Bit("Удалить")); connect(actDelete,SIGNAL(triggered()),this,SLOT(deleteElement())); contextMenu.exec(mapToGlobal(pos)); } } void MainWindow::addNewElement() { QString newElement = qle->text(); QStringList qslNewElement = newElement.split(";"); for(int i=0; i<qslNewElement.count(); i++) { QListWidgetItem* qlwiItem = new QListWidgetItem(); QString name = qslNewElement.at(i).simplified(); if(name.isEmpty()) continue; qlwiItem->setText(name); qlwiItem->setFlags(qlwiItem->flags () | Qt::ItemIsEditable); qlw->insertItem(qlw->count()-1, qlwiItem); } qle->clear(); qlw->scrollToBottom(); } void MainWindow::editElement() { if (qlw->selectedItems().count() < 1) return; if(qlw->selectedItems().at(0) == qlw->item(qlw->count()-1)) return; emit qlw->editItem(qlw->selectedItems().at(0)); } void MainWindow::deleteElement() { if (qlw->selectedItems().count() < 1) return; if(qlw->selectedItems().at(0) == qlw->item(qlw->count()-1)) return; delete(qlw->selectedItems().at(0)); }
[edit: added missing coding tags: three backstick ``` before and after SGaist]
-
Hi,
Just a wild guess but you might be resizing it to 0