我有这个示例代码:
QDialog *dialog = new QDialog(this);
QPoint dialogPos = dialog->mapToGlobal(dialog->pos());
QPoint thisPos = mapToGlobal(this->pos());
dialog->exec();
Run Code Online (Sandbox Code Playgroud)
但是Dialog并不以他的父母为中心.提前致谢.
更新:
我在MainWindow中从构造函数调用Dialog:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->panelInferior = new WidgetTabsInferior;
this->acciones = new Acciones(this);
crearAcciones();
crearBarraMenu();
crearToolbar();
crearTabsEditor();
crearArbolDir();
crearDockWindows();
crearStatusBar();
setWindowIcon(QIcon(":imgs/logo.png"));
connect(this->pestanasEditor , SIGNAL(currentChanged(int)),this,SLOT(cambioTab(int)));
this->dialogo = new AcercaDe(this);
this->dialogo->move(x() + (width() - dialogo->width()) / 2,
y() + (height() - dialogo->height()) / 2);
this->dialogo->show();
this->dialogo->raise();
this->dialogo->activateWindow();
}
Run Code Online (Sandbox Code Playgroud)
但我得到的是:
我不确定我是否正确地问这个问题,因为我是C++和Qt的新手.但是,假设我有一个QWidget的子类:
class childofqwidget : public QWidget
Run Code Online (Sandbox Code Playgroud)
我可以将指向子类对象的指针传递给QMainWindow的setCentralWidget成员函数吗?像这样的东西:
mainlayout = new childofqwidget;
setCentralWidget(mainlayout);
Run Code Online (Sandbox Code Playgroud)
我问的原因是因为我已经创建了一个QWidget的子类,它有一个带有文本框和一些按钮的布局.我想将其作为QMainWindow对象的中心窗口小部件插入.这可能吗?如果不是,我应该采取更好的方式吗?
为什么QMainWindow的析构函数不是虚拟的?如果我从QMainWindow继承一个类MainWindow并且有一些指针属性,我就无法使用MainWindow的析构函数删除内存.
好吧,我是Qt的新手,但对c ++并不是很新的错误对于递归包含有效但看看我的代码我没有看到任何递归包含...我认为这是其他任何人都会帮助我跟踪错误:
'{'token {之前的预期类名
这是我的代码:
#ifndef COLE_H
#define COLE_H
#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class COLE;
}
class COLE : public QDialog , public Ui::MainWindow
{
Q_OBJECT
public:
explicit COLE(QWidget *parent = 0);
~COLE();
private slots:
void on_pushButton_clicked();
public:
double var1;
private:
Ui::COLE *ui;
};
#endif // COLE_H
Run Code Online (Sandbox Code Playgroud)
我知道问题来自"public Ui :: MainWindow",但我确信这应该如何.我检查了我班级的名字,它是MainWindow.
请帮忙.
我是Qt编程的新手,并且遵循了本教程.
在本教程中,地球上有一个复杂的程序,它是这样的:
#include "mainwindow.h"
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication prog(argc, argv);
QLabel *l = new QLabel("Itay's First GUI in C++!!");
l->show();
return prog.exec();
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我得到这些错误:
C:\Users\user-pc\Tutorial\main.cpp:6: error: variable 'QApplication prog' has initializer but incomplete type
QApplication prog(argc, argv);
^
C:\Users\user-pc\Tutorial\main.cpp:10: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我这个原因吗?
我有一个扩展的自定义窗口小部件QMainWindow。我QAction在菜单栏上添加了多个s以及每个快捷键,它们可以正常工作。现在,我想从菜单栏中删除其中的一些操作,但是我想保持快捷方式处于启用状态(用户可以从“ 帮助”对话框中了解快捷方式的可用性)。所以首先我决定将这些动作隐藏起来。
那是行不通的,所以我想如果快捷方式必须起作用,那么动作就不会是不可见的。所以我将其添加到主窗口中,但仍然无法正常工作。任何想法,我如何使它起作用?这是我的代码。该方法中有什么需要发生的someMethod。
class MyWidget: public QMainWindow {
public:
MyWidget();
};
MyWidget::MyWidget() {
QAction *myAct = new QAction(tr("&Some Text"), this);
fNextmyActPageAct->setShortcut(QKeySequence(Qt::Key_Right));
myAct->setVisible(false); //adding this does not work
connect(myAct, SIGNAL(triggered()), this, SLOT(someMethod()));
...
QMenu *someMenu = menuBar()->addMenu(tr("&Some Menu"));
someMenu->addAction(myAct); //this works, the option shows up in the menu 'Some Menu' and the shortcut works
this->addAction(myAct); //does not work
}
Run Code Online (Sandbox Code Playgroud) 我是Qt的新手,我很难找到一个简单的例子来说明如何在主窗口上显示一些文字.例如,我只想在字符串中保存一些文本并在主窗口中显示内容.我想在这方面做这样的事mainwindow.cpp但无济于事.
this->setText("Hello, world!\n");
Run Code Online (Sandbox Code Playgroud) 我找到了一个在无框窗口上设置边框的示例,但是它不是可拖动的。如何使无框窗口可拖动?特别是如果我能看到一个例子,那就太好了。这是我的示例代码(通常代码更长,这就是为什么很多库不介意的原因);
from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
QTextEdit,QDialog,QFrame,QProgressBar
)
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer
import sys
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
#size
self.setFixedSize(320, 450)
self.center
#label
self.lbl = QLabel(self)
self.lbl.setText("test")
self.lbl.setStyleSheet("background-color: rgb(0,0,0);"
"border: 1px solid red;"
"color: rgb(255,255,255);"
"font: bold italic 20pt 'Times New Roman';")
self.lbl.setGeometry(5,5,60,40)
self.show()
#center
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft()) …Run Code Online (Sandbox Code Playgroud) 为什么当我将 QMainWindow 的 SetWindowFlags 切换为 WindowStaysOnTopHint 时,我的窗口会消失,更重要的是不会留在顶部?我正在使用 PySide 和 QMainWindow。
import sys
from PySide import QtGui, QtCore
class Browser(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Browser, self).__init__(parent)
self.resize(200, 150)
self.setWindowTitle('Assets')
self.initUI()
def initUI(self):
self.mi_stay_on_top = QtGui.QAction('Stay On Top', self)
self.mi_stay_on_top.setShortcut('Ctrl+T')
self.mi_stay_on_top.setCheckable(True)
self.mi_stay_on_top.triggered.connect(self.toggle_stay_on_top)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(self.mi_stay_on_top)
grid = QtGui.QVBoxLayout()
grid.setContentsMargins(10,10,10,10)
self.setLayout(grid)
def toggle_stay_on_top(self):
if self.mi_stay_on_top.isChecked():
# enabled
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
else:
# disable
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
def main():
app = QtGui.QApplication(sys.argv)
ex = Browser()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__': …Run Code Online (Sandbox Code Playgroud) 我有一个错误,在关闭我的应用程序后,我收到一条错误消息,提示它已意外停止。我达到了:
但是,关闭窗口后,出现此错误:
该程序意外完成。该过程被迫结束。
我的项目结构是:
我的代码是:
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <windows/login.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Login w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
util_window.h
#ifndef UTIL_H
#define UTIL_H
#include <QObject>
#include <QMainWindow>
class UtilWindow : QObject
{
public:
// vars
// methods
util();
void setCenterWindow(QMainWindow* w);
};
#endif // UTIL_H
Run Code Online (Sandbox Code Playgroud)
util_window.cpp
#include "util_window.h"
#include <QDesktopWidget>
UtilWindow::util()
{
}
void UtilWindow::setCenterWindow(QMainWindow *w) {
int width = w->frameGeometry().width();
int height = w->frameGeometry().height();
QDesktopWidget wid;
int screenWidth = wid.screen()->width(); …Run Code Online (Sandbox Code Playgroud)