Honestly I didn't get how to achieve this using QWidget::mapToGlobal(QPoint). I think this can be very straight forward but I would be glad if you say a short example with it.
Originally Posted by
prasad_N
QWidget::mapToGlobal()
Honestly I didn't get how to achieve this using QWidget::mapToGlobal(QPoint). I think this can be very straight forward but I would be glad if you say a short example with it.
Added after 9 minutes:
Thanks to all
I used all replies to find the solution.
actually I want to read position at start time from file, If file not exist, I set it to an arbitrary position like (50,50). When closing, I write current position to the file to get it next time and show the window in prev position. I use closeEvent to achieve this. The reason I get the correct position is that setGeometry() is called at start up, even if the file not exist.
this is the code:
ui(new Ui::M)
{
ui->setupUi(this);
if(QFile::exists("c.ccf")) {
f.close();
r.setX(x);
r.setY(y);
setGeometry(r);
}
else
{
f.close();
r.setX(50); r.setY(50);
setGeometry(r);
}
}
{
f.close();
}
M::M(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::M)
{
ui->setupUi(this);
if(QFile::exists("c.ccf"))
{
QFile f("c.ccf");
f.open(QIODevice::ReadOnly);
QString str = f.readAll();
f.close();
QStringList lst = str.split(",");
int x = QString(lst.at(0)).toInt(), y = QString(lst.at(1)).toInt();
QRect r = geometry();
r.setX(x);
r.setY(y);
setGeometry(r);
}
else
{
QFile f("c.ccf");
f.open(QIODevice::WriteOnly);
f.close();
QRect r = geometry();
r.setX(50); r.setY(50);
setGeometry(r);
}
}
void M::closeEvent(QCloseEvent *e)
{
QRect r = geometry();
QFile f("c.ccf");
f.open(QIODevice::WriteOnly);
QTextStream out(&f);
out << QString::number(r.x()) + "," + QString::number(r.y()) << endl;
f.close();
}
To copy to clipboard, switch view to plain text mode
Bookmarks