Qt - widget - update

I have a widget with a button. I want each click on the button in the widgets to add one shortcut. I give the code below, but not working. I do not know why. Does anyone help me?

class EditThingsWindow:public QWidget { Q_OBJECT QPushButton * add; public: EditThingsWindow(); public slots: void addButtonClicked(); }; EditThingsWindow::EditThingsWindow():QWidget() { QWidget* wid = this; wid->resize(400,400); add=new QPushButton(wid); add->setText("Add"); add->move(20,10); line=new QLineEdit(wid); line->move(30,50); QObject::connect(add,SIGNAL(clicked()),this,SLOT(addButtonClicked())); } void EditThingsWindow::addButtonClicked() { QLabel* label = new QLabel(this); label->move(200,160); label->setText(";;;;;;;;;;;;;;"); } 
+1
c ++ user-interface qt
source share
2 answers

A new QLabel is really added to EditThingsWindow every time you click on a button. However, since the labels do not fit in the layout and they all move in the same place with the same text (hence the same size), they all appear on top of each other, and you can only see the top one, probably why you think that they are not added.

Add the layout to the EditThingsWindow widget and add each new QLabel to the layout and you will see all the shortcuts.

+5
source share

Just add a layout and put your newborn shortcuts in it.

  QHBoxLayout *layout = new QHBoxLayout; // or some another QLayout descendant layout->addWidget(newWidget); widget->setLayout(layout); 

The only place I had to change was to add a layout to the Widget, and then

 void EditThingsWindow::addButtonClicked() { QLabel * label=new QLabel(this); layout->addWidget(label); label->move(200,160); label->setText(";;;;;;;;;;;;;;"); } 

Done.

If you MUST (not do!) A mess with absolute positioning, you must do all of these code templates yourself. Headings and attachments omitted.

 int main(int argc, char *argv[]) { QApplication a(argc, argv); EditThingsWindow w(0); w.show(); return a.exec(); } EditThingsWindow::EditThingsWindow(QWidget *parent):QWidget(parent) { i = 0; setGeometry(2, 2, 400, 400); add=new QPushButton(this); add->setGeometry(2, 2, 100, 20); add->setText("Add"); add->move(20,10); QObject::connect(add,SIGNAL(clicked()),this,SLOT(addButtonClicked())); } void EditThingsWindow::addButtonClicked() { QLabel * label=new QLabel(this); label->setGeometry(10, 30 + i* 30, 50, 20); i++; label->setText(";;;;;;;;;;;;;;"); label->show(); } 
+2
source share

All Articles