Why there is a line on my dialog?
-
Hi, when I add the line QObject::connect(&w, &window::resized, &receiverWindow, &Receiver::resizeWindow); to my main.cpp, there is a line appearing on my window which is undesired. I wonder why is this. What I want to do here is to connect my window to receiver so that when window is resized, the receiver will automatically resize as well.
//connection in main.cpp QObject::connect(&w, &window::resized, &receiverWindow, &Receiver::resizeWindow); //emit a signal when the window is resized void window::resizeEvent(QResizeEvent *e) { Q_UNUSED(e); windowSize -> setWidth(width()); windowSize -> setHeight(height()); emit resized(windowSize); } //a slot which resizes the receiver's window size void Receiver::resizeWindow(QSize* size) { resize(*size); }
-
In
resizeEvent
use the size that comes with the event:e->size()
instead ofwidth()
/height()
.When overriding events don't forget to call base implementation or you might experience glitches.
QSize is the same size as a pointer so it's no less performant and a lot safer to pass it by value, or, if you don't want to do that, then by const reference.
-
void window::resizeEvent(QResizeEvent *e) { Q_UNUSED(e); QSize windowSize = e->size(); emit resized(windowSize); } void Receiver::resizeWindow(QSize size) { resize(size); }
Despite changing the code to the code above, the line still appears on the window. It seems that by simply connecting these two dialogs will result in the appearance of line which I have no clue why is that.
-
You skipped the middle part :) Call base implementation of
resizeEvent
. I don't know the base implementation, but I would suspect it causes update and subsequent repaint of the window. Your receiver probably has a position slightly offset from the top and if you don't call base resizeEvent that part is not repainted.Also
e
is now used so you don't needQ_UNUSED
anymore. -
Even though I mark the function in the class as
void resizeEvent(QResizeEvent *e) override;
which I think is what you mean by overriding the base class , there is still a line appearing on the screen. -
Hi
The override keyword just makes the compiler complain if you do not override anything . (misspelled name, parameters wrong etc)You still need to call the "original" code which we talk about as " Call base implementation"
In code it would be something like
void window::resizeEvent(QResizeEvent *e)
{
QSize windowSize = e->size();
emit resized(windowSize);
QMainWindow::resizeEvent(e); // calling the code we "overrode"
}This assumes that your class window is a subclass of QMainWindow.
Else the " QMainWindow::" part should be replaced with the real base class name. -
Thank you for your reply. Despite that I call the base implementation the line still appears...
/*My code now*/ void window::resizeEvent(QResizeEvent *e) { QSize windowSize = e->size(); emit resized(windowSize); QDialog::resizeEvent(e); }
-
Can you explain a little more what you are trying to achieve? Are your
window
andReceiver
objects two separate windows? Or is one contained within the other?And from which Qt classes are
window
andReceiver
derived? -
@SimonSchroeder Hi, window and sender are two separate dialogs and there is no parent-child relation between them. However, they are connected through signals and slots. What I want to do here is when I resize the window dialog, which is the main interface between the user and the program, the receiver dialog will be automatically resized. The problem here is that a line suddenly appears on my window dialog not my receiver's when I add this line
QObject::connect(&w, &Sender::resized, &receiverWindow, &Receiver::resizeWindow);
to my code. Please tell me if you need more code to debug this. Thank you :) -
I was being very stupid here, the line appears on my screen is actually a QRect which I accidently draw it in paintevent. Sorry for taking up your guys time to solve this issue.
-
Well it was good you found it. No worries.
Calling the base version is ultra important in most cases when using Qt so
if you walk away with that knowledge extra, it was all worth it :)