PDA

View Full Version : Frameless Widget move



mouni
11th August 2016, 07:00
Hi,


I am trying to move frameless widget according to screen geometry .... i want xpos constant 175 and y pos should move can anybody give some code....

Fivezero05
11th August 2016, 11:40
Do you mean you just want to spawn buttons/labels/... on the screen at will? Because then you are in luck :p I wasted a complete day figuring that out :p


frmMain is my QMainWindow. btnExtraButton is a button i can press to add a extra button on frmMain. btnRemveAll removes them all.
oListButton is defined by (in frmMain.h) :


using namespace std;
...
vector<QPushButton*> oListButton;
...
private slots:
void oTest();




void frmMain::on_btnExtraButton_clicked()
{
QPushButton *buttonA = new QPushButton(this); //create new button, with this as parent

oListButton.insert(oListButton.end(), buttonA); // use a normal std::vector to store the button, so I can later check which button was pressed

int iXlocation = 50;
int iYlocation = 50;
int iHeight = 100;
int iWidth = 100;
buttonA->setGeometry(iXlocation , iYlocation , iHeight , iWidth ); // coordinate (0,0) is in the left upper corner. Maybe i switched height en width, you'll find out soon enough

buttonA->show(); // so you can actually see it

QObject::connect(oListButton[oListButton.size() - 1], SIGNAL(clicked()), this, SLOT(oTest())); //add the last button in the vector to a connect (just so something happened when it got pressed)
}

void frmMain::on_btnRemoveAll_clicked()
{
for (int a = 0; oListButton.size() > a; a++)
{
QObject::disconnect(oListButton[a], SIGNAL(clicked()), this, SLOT(oTest()));
oListButton[a]->hide();
oListButton[a]->deleteLater();
}
oListButton.clear();
}

void frmMain::oTest()
{
QObject* obj = sender(); //obj is now the object of the sender, which i will find in my vector with a for-loop
int iTest = -1;
for (int a = 0; oListButton.size() > a; a++)
{
if (oListButton[a] == sender())
{
iTest = a;
}
}

}

anda_skoa
11th August 2016, 13:25
In your "clear all" slot you can just use qDeleteAll, to delete all buttons


qDeleteAll(oListButton);
oListButton.clear();


In the oTest slot you can also cast the sender instead of looping through the container if you need the object by its actual type


QPushButton *button = qobject_cast<QPushButton*>(sender());


If you just need the index, then I would suggest using a QVector instead of std::vector and using QVector::indexOf().

Cheers,
_