Qt 小部件似乎与其他小部件重叠,而它们不应该重叠

发布于 2024-09-28 00:31:44 字数 1436 浏览 0 评论 0原文

我使用 setGeometry() 来移动窗口上的一些按钮和列表并调整其大小。 问题在于按钮和列表有一定的顺序并且奇怪地重叠。我什至不希望它们重叠,也不明白它们为什么重叠。

正如它们在下面的代码中一样,我唯一可以单击的是 BtnPlay。鼠标悬停时另一个按钮甚至没有改变。根据我如何放置它们,事情变得可点击。

有一次,我首先创建了最右边的小部件,最后创建了在原点的小部件。一切正常,这让我相信它们在看不见的平原上重叠,但我不明白为什么或如何解决它。 此外,当我在列表附近放置一个按钮(该按钮位于列表的左侧)时,由于某种原因,该列表不再可单击。 设置几何图形允许我设置小部件的左上角坐标、宽度和高度。我不明白为什么他们会互相干扰。

 int Gui_Init(int argc, char *argv[])
 {
 QApplication app(argc, argv);
 app.setStyle("plastique");
 QWidget Window;
 Window.resize(800, 600);

 QTrackList = new QListObj(&Window);
 RebuildButton BtnRB(&Window);
 PlayButton BtnPlay(&Window);

 Window.show();
 return app.exec();
 return 0;
 }


 RebuildButton::RebuildButton(QWidget *parent) : QWidget(parent)
 {
 Rebuild = new QPushButton(tr("Rebuild Library"), this);
 Rebuild->setGeometry(400,575,100,25);
 connect(Rebuild, SIGNAL(clicked()), this, SLOT(RebuildLibrary()));
 }


 PlayButton::PlayButton(QWidget *parent) : QWidget(parent)
 {
 PlayBtn = new QPushButton(tr("Play Track"), this);
 PlayBtn->setGeometry(400, 550, 100, 25);
 connect(PlayBtn, SIGNAL(clicked()), this, SLOT(PlayTrack()));
 }

QListObj 的构造函数:

 QListObj::QListObj(QWidget *parent) : QWidget(parent)
 {
 List = new QListWidget(parent);
 List->setGeometry(500,0,300,600);
 new QListWidgetItem(tr("fix it"), List);
 connect(List, SIGNAL(itemSelectionChanged()), this, SLOT(SelectTrack()));
 }

I used setGeometry() to move and size some buttons and a list on a Window.
The problem is that the buttons and list have a certain order and are overlapping strangely. I don't even want them to overlap and don't understand why they are overlapping in the first place.

As they are in the code below, the only thing I can click is BtnPlay. The other button isn't even changing on a mouse over. Depending on how I position them things become click-able.

There was a point where I had the widget created first, furthest to the right, and the widget created last at the origin. Everything worked, which leads me to believe that they are overlapping on an unseen plain, but I don't understand why or how to fix it.
In addition, When I put a button near the list (the button was on the left side of the list), for some reason the list was no longer click able.
Set geometry allows me to set the top left co-ordinates, the width, and height of the widget. I don't understand why they would interfere with each other.

 int Gui_Init(int argc, char *argv[])
 {
 QApplication app(argc, argv);
 app.setStyle("plastique");
 QWidget Window;
 Window.resize(800, 600);

 QTrackList = new QListObj(&Window);
 RebuildButton BtnRB(&Window);
 PlayButton BtnPlay(&Window);

 Window.show();
 return app.exec();
 return 0;
 }


 RebuildButton::RebuildButton(QWidget *parent) : QWidget(parent)
 {
 Rebuild = new QPushButton(tr("Rebuild Library"), this);
 Rebuild->setGeometry(400,575,100,25);
 connect(Rebuild, SIGNAL(clicked()), this, SLOT(RebuildLibrary()));
 }


 PlayButton::PlayButton(QWidget *parent) : QWidget(parent)
 {
 PlayBtn = new QPushButton(tr("Play Track"), this);
 PlayBtn->setGeometry(400, 550, 100, 25);
 connect(PlayBtn, SIGNAL(clicked()), this, SLOT(PlayTrack()));
 }

The Constructor for QListObj:

 QListObj::QListObj(QWidget *parent) : QWidget(parent)
 {
 List = new QListWidget(parent);
 List->setGeometry(500,0,300,600);
 new QListWidgetItem(tr("fix it"), List);
 connect(List, SIGNAL(itemSelectionChanged()), this, SLOT(SelectTrack()));
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-10-05 00:31:44

您的设计有点不传统,将 QWidget 子类化以包含按钮,但我怀疑您的问题可能是您正在相对于包含的 QWidget 子类设置按钮的几何形状(RebuildButtonPlayButton),但不设置 RebuildButtonPlayButton 小部件本身的几何形状。

当您对事物的工作方式感觉更舒服时,您可能需要重新设计一下,并尝试将 GUI 与业务逻辑分开。创建 QWidget 的子类作为应用程序窗口(或使用 QMainWindow),并使用布局管理器的组合在该子类构造函数中添加/布局所有 gui 控件。

然后,我建议您将所有按钮信号连接到窗口类中的私有插槽,并从这些插槽发出自定义信号,触发业务逻辑在其他地方执行。

Your design is a bit unconventional, subclassing QWidget to contain a button, but I suspect your problem may be that you are setting the geometry for the buttons relative to the containing QWidget subclasses (RebuildButton and PlayButton) but not setting the geometry for the RebuildButton and PlayButton widgets themselves.

When you feel more comfortable with how things work, you may want to redesign a bit and try to separate your gui from your business logic. Create a subclass of QWidget to act as your application window (or use QMainWindow) and use a combination of layout managers to add/layout all of your gui controls in that subclass constructor.

I would then suggest you make all of your button signal connections to private slots in the window class and from those slots, emit custom signals that trigger business logic to execute elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文