I've spent a couple days trying to chase down this issue and cannot seem to figure it out. My application has a frame which plays parent to a set of QPushButtons that are created during program execution in response to user input. The QPushButtons will have some additional functionality, including drag/drop capabilities so, (a) they're part of a class (called solenoid ) that is subclassed from QWidget, and (b) I've implemented mousepressevent().
The QFrame is created using QT Designer, and I want to handle the button positioning within it myself (i.e. no layout). For now, I have a QPushButton elsewhere that creates the solenoid objects within the QFrame using the following code:
void MainWindow::on_cmdMakeSols_clicked()
{
int i;
solenoid* testSolenoid[8] = {0,0,0,0,0,0,0,0};
for(i = 0; i < 8; i++)
{ testSolenoid[i] = new solenoid(ui->boxSolenoidContainer, 500 + (i*30), 5, 25, 40, (i+1));
testSolenoid[i]->show();
}
return;
}
void MainWindow::on_cmdMakeSols_clicked()
{
int i;
solenoid* testSolenoid[8] = {0,0,0,0,0,0,0,0};
for(i = 0; i < 8; i++)
{ testSolenoid[i] = new solenoid(ui->boxSolenoidContainer, 500 + (i*30), 5, 25, 40, (i+1));
testSolenoid[i]->show();
}
return;
}
To copy to clipboard, switch view to plain text mode
And the constructor for solenoid:
solenoid
::solenoid(QWidget *parent,
int xPos,
int yPos,
int width,
int height,
int ID_number
) :{
solenoidNumber = ID_number;
baseXpos = xPos;
baseYpos = yPos;
xSize = width;
ySize = height;
clickButton.setParent(parent);
clickButton.setGeometry(xPos, yPos, (width - 2), (height - 2));
clickButton.show();
this->setMouseTracking(true);
this->update();
}
solenoid::solenoid(QWidget *parent, int xPos, int yPos, int width, int height, int ID_number) :
QWidget(parent)
{
solenoidNumber = ID_number;
baseXpos = xPos;
baseYpos = yPos;
xSize = width;
ySize = height;
clickButton.setParent(parent);
clickButton.setGeometry(xPos, yPos, (width - 2), (height - 2));
clickButton.show();
this->setMouseTracking(true);
this->update();
}
To copy to clipboard, switch view to plain text mode
The problem I'm having is that there is an invisible region in the top left of the QFrame, about 130x30 pixels, which triggers the onmousepress() event for the final solenoid object created when I click within it. It sits atop any solenoid objects created in that space.
Through experimentation, I have found that the invisible region is created when the QWidget(parent) constructor is called. Can anyone fill me in on what's going on here?! Is there a purpose for this invisible region? Is it part of the QFrame, or the object itself? Most importantly, how do I get rid of it?
Hope my description has been clear; any guidance is much appreciated!
EDIT: if I call testSolenoid[7].rect(); it returns a rectangle with coordinates (0,0) and (100,30), which corresponds to the "invisible spot" I'm referring to, although the widget is drawn where I expect it and responds to click events where it is drawn. I tried calling the updateGeometry() and update() methods after creating the objects, but see no change in behavior.
Bookmarks