سؤال

I seem to be having an issue. Objective: I want to dynamically add QTextEdit to a QMainWindow, I have a lot of data I wish to split amongst various QTextEdit objects. I've been looking at centralWidget and did some digging into ui->setupUi(this); generated by the Qt Creator and spotted that the parent for objects of interest was the central widget of the QMainWindow. Thus I've tried something like this:

this->m_vecTextEdits.push_back( new QTextEdit(this->centralWidget()) );

where 'this' is the QMainWindow. I just want to add these QTextEdit to the QMainWindow and later remove them. I also tried new QTextEdit(this) hoping it would appear on the QMainWindow with the properties defined by the objects geometry to no luck.

If I setCentralWidget to be that of the QTextEdit than it works but I don't want the object to consume the entire QMainWindow and restrict access to existing widgets.

So I'm in need of advice of basically how I can add QTextEdit widgets to the existing centralWidget of the QMainWindow and have them appear in the window and also remove.

هل كانت مفيدة؟

المحلول

I wanted to add multiple QTextEdit so I can use a residing QListWidget (the index property) to switch amongst the many QTextEdit widgets

You could put a QStackedWidget in place of your QTextEdit, and add all the QTextEdits to it.
Only one of the textedits would be visible at all time, but you can switch between them automatically by connecting the signal currentRowChanged(int) of your QListWidget to the slot setCurrentIndex(int) so that the index of the QTextEdit stay the same as the index of the selected item in your list.

The QStackedWidget will replace your container m_vecTextEdits too.

نصائح أخرى

It's not enough to just create the widget objects; you also need to add them to a layout object. Try something like:

QBoxLayout * bl = new QBoxLayout(centralWidget());
QTextEdit * t = new QTextEdit;
bl->addWidget(t);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top