我花了几天时间做这件事,但我尝试的每一个解决方案都没有用。
我有colorPicker,我选择一种颜色。 ColorPicker是一个类。我在另一个名为ToolWindow的类中有按钮。我想在ColorPicker类中选择颜色后更改ToolWindow类中按钮的颜色。
以下是一些代码:
toolWindow.h
#ifndef TOOLWINDOW_H
#define TOOLWINDOW_H
#include <QDialog>
#include "colorpicker.h"
//#include "mainwindow.h"
namespace Ui {
class ToolWindow;
}
class ToolWindow : public QDialog
{
Q_OBJECT
public:
explicit ToolWindow(QWidget *parent = 0);
~ToolWindow();
void setColor(QColor color);
private slots:
void on_penTool_clicked();
void on_rectTool_clicked();
void on_selectTool_clicked();
void on_circleTool_clicked();
void on_lineTool_clicked();
void on_colorButton_clicked();
private:
Ui::ToolWindow *ui;
ColorPicker colorPicker;
QColor nColor;
};
#endif // TOOLWINDOW_H
toolWindow.cpp
#include "toolwindow.h"
#include "ui_toolwindow.h"
#include "colorpicker.h"
#include "mainwindow.h"
#include <QMouseEvent>
ToolWindow::ToolWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::ToolWindow)
{
ui->setupUi(this);
}
ToolWindow::~ToolWindow()
{
delete ui;
}
void ToolWindow::on_penTool_clicked()
{
mSelection = 1;
}
void ToolWindow::on_rectTool_clicked()
{
mSelection = 2;
}
void ToolWindow::on_selectTool_clicked()
{
mSelection = 0;
}
void ToolWindow::on_circleTool_clicked()
{
mSelection = 3;
}
void ToolWindow::on_lineTool_clicked()
{
mSelection = 4;
}
void ToolWindow::on_colorButton_clicked()
{
colorPicker.show();
}
void ToolWindow::setColor(QColor color)
{
//ui->colorButton->setPalette(color);
//nColor = color;
}
colorPicker.h
#ifndef COLORPICKER_H
#define COLORPICKER_H
#include <QDialog>
#include <QColor>
//#include "mainwindow.h"
namespace Ui {
class ColorPicker;
}
class ColorPicker : public QDialog
{
Q_OBJECT
public:
explicit ColorPicker(QWidget *parent = 0);
~ColorPicker();
QPalette palete;
private slots:
void on_buttonBox_rejected();
void onColorChanged();
void onCMYKChanged();
void on_buttonBox_accepted();
private:
Ui::ColorPicker *ui;
double min(double a,double b,double c);
QColor mColor;
double k,c,m,y;
int r,g,b;
signals:
void colorChanged(QColor arg);
};
#endif // COLORPICKER_H
colorPicker.cpp
#include "colorpicker.h"
#include "ui_colorpicker.h"
#include "toolwindow.h"
#include "mainwindow.h"
#include <QPalette>
ColorPicker::ColorPicker(QWidget *parent) :
QDialog(parent),
ui(new Ui::ColorPicker)
{
ui->setupUi(this);
connect(ui->redSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->greenSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->blueSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->blackSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->yellowSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->magentaSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->cyanSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
//connect(this,SIGNAL(colorChanged(QColor)),toolWindow,SLOT(setColor(QColor)));
onColorChanged();
}
ColorPicker::~ColorPicker()
{
delete ui;
}
void ColorPicker::on_buttonBox_rejected()
{
this->close();
}
double ColorPicker::min(double a,double b,double c)
{
double tmp;
if(a>b)
{
if(a>c)
tmp = a;
else
tmp = c;
}
else
{
if(b>c)
tmp = b;
else
tmp = c;
}
return tmp;
}
void ColorPicker::onColorChanged()
{
int q1,q2,q3;
mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
q1=ui->redSlider->value();
q2=ui->greenSlider->value();
q3=ui->blueSlider->value();
palete = ui->widget->palette();
palete.setColor(QPalette::Window, mColor);
ui->widget->setPalette(palete);
emit colorChanged(mColor);
k=min(q1,q2,q3);
c=(1-ui->redSlider->value()-k)/(1-k);
m=(1-ui->greenSlider->value()-k)/(1-k);
y=(1-ui->blueSlider->value()-k)/(1-k);
ui->blackSlider->setValue(k);
ui->cyanSlider->setValue(c);
ui->magentaSlider->setValue(m);
ui->yellowSlider->setValue(y);
}
void ColorPicker::onCMYKChanged()
{
mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
palete = ui->widget->palette();
palete.setColor(QPalette::Window, mColor);
ui->widget->setPalette(palete);
emit colorChanged(mColor);
}
void ColorPicker::on_buttonBox_accepted()
{
//QPalette pal = ui->widget->palette();
//pal.setColor(QPalette::Window, mColor);
//Ui::ToolWindow
//MainWindow::MainWindow().mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
//mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
}
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "toolwindow.h"
namespace Ui {
class MainWindow;
}
class QPainter;
class QImage;
extern int mSelection;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
ToolWindow toolWindow;
QColor mColor;
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
Ui::MainWindow *ui;
QPainter *mPainter;
QImage *mImage;
QPixmap mPix;
QPoint mBegin;
QPoint mEnd;
QRect mRect;
QLine mLine;
bool drawStarted;
bool mPressed;
int mSize;
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QImage>
int mSelection = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mPix = QPixmap(400,400);
mPix.fill(Qt::white);
mPressed = false;
drawStarted = false;
mColor = QColor(Qt::black);
mSize = 2;
toolWindow.setWindowFlags(Qt::SubWindow | Qt::WindowDoesNotAcceptFocus);
toolWindow.show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,mPix);
QPen pen(mColor);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(mSize);
painter.setPen(pen);
if(mPressed)
{
if(mSelection == 2)
painter.drawRect(mRect);
else if(mSelection == 4) {
painter.drawLine(mLine);
}
drawStarted = true;
} else if (drawStarted){
QPainter tempPainter(&mPix);
tempPainter.setPen(pen);
if(mSelection == 2)
tempPainter.drawRect(mRect);
else if(mSelection == 4) {
tempPainter.drawLine(mLine);
}
painter.drawPixmap(0,0,mPix);
}
painter.end();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
mPressed = true;
mBegin = event->pos();
if(mSelection == 2){
mRect.setTopLeft(event->pos());
mRect.setBottomRight(event->pos());
}
else if(mSelection == 4){
mLine.setP1(event->pos());
mLine.setP2(event->pos());
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QPen pen(mColor);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(mSize);
switch(mSelection)
{
case 1:
{
QPainter penPainter(&mPix);
penPainter.setPen(pen);
mEnd = event->pos();
penPainter.drawLine(mBegin, mEnd);
mBegin = mEnd;
update();
break;
}
case 2:
{
mRect.setBottomRight(event->pos());
update();
break;
}
case 4:
{
mLine.setP2(event->pos());
update();
break;
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
mPressed = false;
update();
}
的main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
MainWindow w;
w.show();
return a.exec();
}
colorPicker.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ColorPicker</class>
<widget class="QDialog" name="ColorPicker">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>355</width>
<height>293</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>R</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>G</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>B</string>
</property>
</widget>
<widget class="QSlider" name="redSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>10</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="greenSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="blueSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>70</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSpinBox" name="redSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>8</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="greenSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>40</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="blueSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>70</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>K</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>10</x>
<y>160</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Y</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>10</x>
<y>190</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>M</string>
</property>
</widget>
<widget class="QSlider" name="blackSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>130</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="yellowSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>160</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="magentaSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>190</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>260</y>
<width>156</width>
<height>23</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>230</x>
<y>10</y>
<width>101</width>
<height>271</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>76</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>10</x>
<y>220</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
<widget class="QSlider" name="cyanSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>220</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="singleStep">
<number>1</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSpinBox" name="blackSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>130</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="yellowSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>160</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="magentaSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>190</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="cyanSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>220</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ColorPicker</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>291</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ColorPicker</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>290</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>redSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>redSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>23</y>
</hint>
<hint type="destinationlabel">
<x>193</x>
<y>22</y>
</hint>
</hints>
</connection>
<connection>
<sender>redSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>redSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>11</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>16</y>
</hint>
</hints>
</connection>
<connection>
<sender>greenSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>greenSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>52</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>53</y>
</hint>
</hints>
</connection>
<connection>
<sender>greenSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>greenSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>182</x>
<y>46</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>46</y>
</hint>
</hints>
</connection>
<connection>
<sender>blueSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>blueSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>80</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>79</y>
</hint>
</hints>
</connection>
<connection>
<sender>blueSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>blueSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>179</x>
<y>81</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>78</y>
</hint>
</hints>
</connection>
<connection>
<sender>cyanSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>cyanSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>178</x>
<y>227</y>
</hint>
<hint type="destinationlabel">
<x>144</x>
<y>225</y>
</hint>
</hints>
</connection>
<connection>
<sender>cyanSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>cyanSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>143</x>
<y>236</y>
</hint>
<hint type="destinationlabel">
<x>193</x>
<y>241</y>
</hint>
</hints>
</connection>
<connection>
<sender>magentaSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>magentaSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>144</x>
<y>202</y>
</hint>
<hint type="destinationlabel">
<x>170</x>
<y>202</y>
</hint>
</hints>
</connection>
<connection>
<sender>magentaSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>magentaSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>191</x>
<y>191</y>
</hint>
<hint type="destinationlabel">
<x>143</x>
<y>195</y>
</hint>
</hints>
</connection>
<connection>
<sender>yellowSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>yellowSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>146</x>
<y>171</y>
</hint>
<hint type="destinationlabel">
<x>185</x>
<y>175</y>
</hint>
</hints>
</connection>
<connection>
<sender>yellowSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>yellowSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>181</x>
<y>168</y>
</hint>
<hint type="destinationlabel">
<x>122</x>
<y>170</y>
</hint>
</hints>
</connection>
<connection>
<sender>blackSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>blackSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>148</x>
<y>142</y>
</hint>
<hint type="destinationlabel">
<x>183</x>
<y>143</y>
</hint>
</hints>
</connection>
<connection>
<sender>blackSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>blackSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>136</y>
</hint>
<hint type="destinationlabel">
<x>143</x>
<y>136</y>
</hint>
</hints>
</connection>
</connections>
</ui>
我达到了这篇文章中使用的字符的限制。
以下是整个项目的链接:http://uploadfile.pl/pokaz/1252644---svem.html
我尝试了带信号和插槽的解决方案,但它对我不起作用 - 我想我做错了。
答案 0 :(得分:0)
要在按下接受对话框的按钮后,使用NCT_ID
获取某些属性的值,则无需使用信号。你必须做的是执行生成主循环的QDialog
函数,这样在它之后就不会执行任何行,这个函数返回我们可以分别用Accepted和Rejected检查的已接受和被拒绝的状态。
但为此你必须创建一个将在ColorPicker中称为颜色的getter方法:
<强> *的.h 强>
exec()
<强> *。CPP 强>
public:
QColor color() const;
然后我们重写QColor ColorPicker::color() const
{
return mColor;
}
的广告位on_colorButton_clicked
和setColor
的函数:
ToolWindow
可以在以下link
找到完整的代码