I'm running a test to understand parent/child relationships in Qt and I have a question about how I can view these relationships in the Qt Creator Debugger.
When I start my demo application, here is what the debugger shows:
Because I call Qt's dumpObjectTree() before I add any widgets, the tree is empty, except for the MainWindow's layout. That's what I expected.
When I close the application, and the ~MainWindow destructor is called, I call dumpObjectTree() again, but this time, all of the widgets that I created show in the tree. If I called dumpObjectTree() after the window was destroyed, shouldn't the tree be empty again?
Am I not destroying the child widgets correctly, or do I misunderstand the information displayed by the dumpObjecTree() function?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
qDebug() << "WINDOW INITIALIZED-------------";
dumpObjectTree();
this->buildLayout();
}
void MainWindow::buildLayout() {
QWidget *window = new QWidget(this);
this->setObjectName("Main Window");
layout = new QVBoxLayout();
QSplitter *split = new QSplitter();
split->setObjectName("Horizontal Split");
split->setOrientation(Qt::Horizontal);
QTextEdit *editor1 = new QTextEdit();
editor1->setObjectName("Editor 1");
QTextEdit *editor2 = new QTextEdit();
editor2->setObjectName("Editor 2");
split->addWidget(editor1);
split->addWidget(editor2);
QSplitter *split2 = new QSplitter();
split2->setObjectName("Vertical Split");
split2->setOrientation(Qt::Vertical);
QTextEdit *editor3 = new QTextEdit();
editor3->setObjectName("Editor 3");
split2->addWidget(split);
split2->addWidget(editor3);
QToolBar *mainToolbar = new QToolBar();
mainToolbar->setObjectName("Main Toolbar");
mainToolbar->addAction("Main Button 1");
mainToolbar->addSeparator();
mainToolbar->addAction("Main Button 2");
mainToolbar->setMovable(true);
layout->addWidget(mainToolbar);
layout->addWidget(split2);
QToolBar *toolbar = new QToolBar(this);
toolbar->setObjectName("Mini Toolbar");
toolbar->addAction("Button 1");
toolbar->addSeparator();
toolbar->addAction("Button 2");
toolbar->setMovable(true);
QMenuBar *menu = new QMenuBar(this);
menu->setObjectName("Menu Bar");
menu->addAction("Menu 1");
menu->addAction("Menu 2");
menu->addAction("Menu 3");
window->setLayout(layout);
MainWindow::addToolBar(toolbar);
MainWindow::setMenuBar(menu);
MainWindow::setCentralWidget(window);
}
MainWindow::~MainWindow()
{
delete layout;
qDebug() << "DESTROYED " << this->metaObject()->className();
qDebug() << "OBJECT TREE-------------";
dumpObjectTree();
qDebug() << "OBJECT INFO-------------";
dumpObjectInfo();
}
Thanks
Your confusion is that when your MainWindow destructor is called, the window is only part of the way through being destroyed.
In particular, the child widgets do not get deleted through the parent-child mechanism until the QObject destructor is called, which occurs after that, so at the point you call dumpObjectTree() all the children still exist.
Related
I built an application that uses QMenu and its related submenu. The Qmenu is triggered to open via QAction.
The problem I have is that as soon as I call the QAction and I chose the QAction that carries the "Options", it should open the QMenu, but instead the QMenu open in another location of the application looking totally detached.
Below is the wrong behavior, where it is possible to see the QMenu opening in another location of the Desktop.
Here is the expected behavior where the QMenu opens up after selecting "Oprions":
I am not sure of why this is happening.
Below the snippet of code related:
rostreewidget.h
class ROSTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
enum ROSType {
NULLPTR,
TABLE,
BASE,
MENU
};
ROSTreeWidget(QWidget *parent = nullptr);
ROSType rostype(ROSTreeItem *rosItem) const;
ROSType rostype() const {
return rostype(currentItem());
}
signals:
void selectFrom();
void editLaserTable();
private:
QAction *mActionSELECT_FROM;
QAction *mActionEditLaserTable;
QAction *mAddMenuOptions;
QMenu *submenuLaserScanA;
QAction *submenuActionA, *submenuActionB, *submenuActionC;
QMenu *submenuLaserScanB;
QAction *submenuActionAA, *submenuActionBB, *submenuActionCC;
QMenu *submenuLaserScanC;
QAction *submenuActionAAA, *submenuActionBBB, *submenuActionCCC;
};
rostreewidget.cpp
ROSTreeWidget::ROSTreeWidget(QWidget *parent) : QTreeWidget(parent)
{
mActionSELECT_FROM = new QAction(QIcon(":"), "SELECT * FROM", this);
mActionEditLaserTable = new QAction(QIcon(":"), "Edit LaserScan Table");
mAddMenuOptions = new QAction(QIcon(":"), "Options", this);
addActions({ mActionSELECT_FROM,
mActionEditLaserTable,
mAddMenuOptions});
connect(mActionSELECT_FROM, &QAction::triggered, [&]() {
emit selectFrom();
});
connect(mActionEditLaserTable, &QAction::triggered, [&]() {
emit editLaserTable();
});
connect(mAddMenuOptions, &QAction::triggered, [&]() {
mMenu = new QMenu();
submenuLaserScanA = mMenu->addMenu( "Menu A" );
mMenu->addSeparator();
submenuLaserScanB = mMenu->addMenu( "Menu B" );
mMenu->addSeparator();
submenuLaserScanC = mMenu->addMenu( "Menu C" );
submenuActionAAA = submenuLaserScanC->addAction("Setup C" );
submenuActionBBB = submenuLaserScanC->addAction( "Setup C" );
mMenu->show();
});
}
What I have done so far:
After following this source I was able to prepare the menu and the submenu to be inserted in the QAction and in fact all the related settings about it are as I need them to be.
I thought that linking the QAction inside the lambda function, exactly as I did for the other choices, would have worked, but instead what I am obtaining as soon as I launch the application is a QMenu that opens in another location of the Desktop without being linked to the main choice as shown below.
According to the documentation QMenu::addMenu parameters set are correct, so what is going on?
Thanks for pointing to the right direction for solving this issue.
Use this as a general pattern:
{
QMenu menu;
menu.addAction(...);
menu.addAction(...);
menu.exec(QCursor::pos()); // exec returns a triggered QAction
// process action
}
QMenu::exec() is modal. You don't need a QMenu instance outside the function.
I have a QVector of QObjects QVector<QWidget*> question_vector;. These widgets are questions. (My application is like a questionnaire thing).
When creating a questionnaire, question types are chosen from the selection on a comboBox, and within Questions class, the question is created, and stored in the QVector.
void CreateSurvey::comboBox_selection(const QString &arg1)
{
if(arg1 == "Single Line Text")
{
Question *singleLineText = new Question("Single Line Text");
surveyLayout->addWidget(singleLineText);
question_vector.append(singleLineText);
qDebug() << "Number of items: "<< question_vector.size();
} ...
}
void Question::create_singleLineEdit()
{
QVBoxLayout *vLayout = new QVBoxLayout;
QLabel *titleLabel = new QLabel("Title");
vLayout->addWidget(titleLabel);
QLineEdit *inputText = new QLineEdit;
vLayout->addWidget(inputText);
QLabel *commentsLabel = new QLabel("Comments");
vLayout->addWidget(commentsLabel);
QLineEdit *commentsText = new QLineEdit;
vLayout->addWidget(commentsText);
ui->frame->setLayout(vLayout);
}
This is what it looks like
The SingleLineEdit is the widget, the title, titleEdit, comments, commentsEdit.
How do I access, for example the text from an individual component of the widget, the commentsText QLineEdit?
Cast the element to the a QLineEdit:
QLineEdit *line_edit = dynamic_cast <QLineEdit *> (question_vector[3]);
if (line_edit)
{
QString text = line_edit->text();
}
This is a basic aspect of C++ programming; you probably should do some reading on C++ classes, how to derive them, how to use base class pointers and derived class pointers, and so on.
I think I've managed to solve what I was trying to do (at least partly)
So I had here
void Question::create_singleLineEdit()
{
QVBoxLayout *vLayout = new QVBoxLayout;
QLabel *titleLabel = new QLabel("Title");
vLayout->addWidget(titleLabel);
QLineEdit *inputText = new QLineEdit;
vLayout->addWidget(inputText);
QLabel *commentsLabel = new QLabel("Comments");
vLayout->addWidget(commentsLabel);
QLineEdit *commentsText = new QLineEdit;
vLayout->addWidget(commentsText);
ui->frame->setLayout(vLayout);
}
What I did was changed stuff like QLineEdit *commentsText = new QLineEdit; to
section_commentsText = newLineEdit; - Having QTextEdit *section_commentsText in my question.h.
I was then able to do
Question *object = question_vector[0];
QString text = object->section_commentsText->text();
qDebug() << text;
I have a QPushButton that will open new window on clicked.
void showNewWindow()
{
PopupWindow *popup = new PopupWindow();
popup->show();
}
And PopupWindow is declared as following:
class PopupWindow : public QWidget {
Q_OBJECT
public:
PopupWindow(QWidget* parent);
void setContent(QString content) { this->content = content; }
QString getContent() { return content; }
private:
QString content;
private slots:
void handleContinue();
void handleRunToEnd();
};
Then I implement a its constructor:
PopupWindow::PopupWindow(QWidget *parent):QWidget(parent)
{
QHBoxLayout *hlayout = new QHBoxLayout();
QWidget *buttonWidget = new QWidget();
QPushButton *btnContinue = new QPushButton();
btnContinue->setText("Continue");
QPushButton *btnRunEnd = new QPushButton();
btnRunEnd->setText("Run till completion");
buttonWidget->setLayout(hlayout);
hlayout->addWidget(btnContinue);
hlayout->addWidget(btnRunEnd);
connect(btnContinue,SIGNAL(clicked()), this, SLOT(handleContinue()));
connect(btnRunEnd,SIGNAL(clicked()), this, SLOT(handleRunToEnd()));
QTextEdit *html = new QTextEdit();
html->setReadOnly(true);
html->setText("AAAA");
QVBoxLayout *layout = new QVBoxLayout();
this->setLayout(layout);
layout->addWidget(html);
layout->addWidget(buttonWidget);
}
My problem: whenever I click on the "Continue" or "Run till completion" buttons on Popup Window. The app crashed. I could see the error as following:
QApplication: Object event filter cannot be in a different thread.
QApplication: Object event filter cannot be in a different thread.
QApplication: Object event filter cannot be in a different thread.
QWidget::repaint: Recursive repaint detected
Please, help me to resolve this.
Ok so what i'm trying to do is to create a new QLabel added to a QList and put it where I clicked on the other QLabel where I clicked.
So here is my code:
class CustomLabel : public QLabel
{
Q_OBJECT
public:
CustomLabel();
void mousePressEvent( QMouseEvent* event);
private:
QList<QLabel *> pointsL;
QList<QPoint *> points;
};
void CustomLabel::mousePressEvent(QMouseEvent *event)
{
points << new QPoint(event->pos());
pointsL << new QLabel(this);
pointsL.at(pointsL.size()-1)->setText("+");
pointsL.at(pointsL.size()-1)->setGeometry(QRect(points.at(points.size()-1)->rx(),, points.at(points.size()-1)->ry(), 1, 1));
}
I also tried:
pointsL.at(pointsL.size()-1)->move(points.at(points.size()-1)->rx(), points.at(points.size()-1)->ry());
and this:
void CustomLabel::mousePressEvent(QMouseEvent *event)
{
points << new QPoint(event->pos());
pointsL << new QLabel(this);
pointsL.at(pointsL.size()-1)->setText("+");
pointsL.at(pointsL.size()-1)->move(*points.at(points.size()-1));
pointsL.at(pointsL.size()-1)->setTabOrder(pointsL.at(pointsL.size()-1), this);
}
When I click on the Custom Label nothing happens. The constructor is empty.
Thanks for any answer.
New widgets added after the parent is already visible on screen should be shown explicitly unless they are in a layout.
So basically you should add:
pointsL.back()−>show();
I have the following hierarchy in my Qt Application:
QMainWindow > QWidget (centralWidget) > QWidget (subclassed) > QLabel
Initialization code in my QMainWindow code:
centralWidget = new QWidget();
centralWidget->setGeometry(0,0,width,height);
chatWidget=new ChatWidget(this); // the subclassed QWidget
setCentralWidget(centralWidget);
In my subclassed QWidget initialization (which happens at the same time than the Qt App initialization) I have the following code:
ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 1"); <-- Is properly Display
}
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2"); <-- Does NOT appear
}
When the QLabel is added from the class initialization then the message is well displayed in the widget.
However when I launch the same code after a button pressed (via a function in the same QWidget subclass), then the text does not appear on screen.
I don't want to use layouts as I need to exactly position my labels.
Tried to repaint, but didn't help neither.
How can I properly and dynamically display a label after the initialization is done ?
Widgets when they are visible for the first time call to be visible to their children, but since you are creating it afterwards they probably are not calling that method, a possible solution is to call the show method.
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2");
lbl->show();
}
comment: it seems strange to me that the QMainWindow you set a central widget and then create the chatWidget as a parent to the QMainWindow, it is generally not recommended to add children to the QMainWindow because it has a given structure, what should be done is to place it inside the centralwidget.
We need to show the label created by button click, cause centralwidget was already painted.
Here is a working example, I added this as answer also I noticed better adding chatWidget as child to centralWidget where in your original code its added to the UI .. this is your choice.
Mainwindow:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//
ui->setupUi(this);
centralWidget = new QWidget();
centralWidget->setGeometry(width,height);
chatWidget=new ChatWidget(centralWidget); // the subclassed QWidget
setCentralWidget(centralWidget);
// added testing
QPushButton *btn = new QPushButton("MyButton",centralWidget);
btn->setGeometry(100,100,100,100);
btn->setMaximumSize(100,100);
connect(btn,&QPushButton::clicked, chatWidget, &ChatWidget::displayChatAfterButtonPressed);
}
and chatWidget:
ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 1");
}
void ChatWidget::displayChatAfterButtonPressed()
{
QLabel *lbl;
lbl=new QLabel(this);
lbl->setText("Hello World 2");
lbl->show();
}